frshjb373 Posted January 10, 2013 Share Posted January 10, 2013 Trying to pass some data through url using some javascript. One of the records in my mysql database contains an ampersand. Not really sure how to encode the url. Any help is much appreciated! Here's the JS I'm using: function reload(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='sell-iphone.php?cat=' + val ; } And here's the PHP. "$cat" is the value I'm pulling from the database. Dont' think the db is relevant to this however: <?php @$cat=$_GET['cat']; // Use this line or below line if register_global is off if(strlen($cat) < 0){ // to check if $cat is numeric data or not. echo "Data Error"; exit; } ?> Link to comment https://forums.phpfreaks.com/topic/272926-passing-a-database-record-with-ampersand-through-javascript-url/ Share on other sites More sharing options...
haku Posted January 10, 2013 Share Posted January 10, 2013 If you are passing data from a PHP script to your javascript, you can use the PHP urlencode() function the text before passing it. If you are passing data from javascript to PHP you can use javascript encodeURI() function on the text before passing it. Link to comment https://forums.phpfreaks.com/topic/272926-passing-a-database-record-with-ampersand-through-javascript-url/#findComment-1404590 Share on other sites More sharing options...
The Letter E Posted January 15, 2013 Share Posted January 15, 2013 Trying to pass some data through url using some javascript. One of the records in my mysql database contains an ampersand. Not really sure how to encode the url. Any help is much appreciated! Here's the JS I'm using: function reload(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='sell-iphone.php?cat=' + val ; } And here's the PHP. "$cat" is the value I'm pulling from the database. Dont' think the db is relevant to this however: <?php @$cat=$_GET['cat']; // Use this line or below line if register_global is off if(strlen($cat) < 0){ // to check if $cat is numeric data or not. echo "Data Error"; exit; } ?> function reload(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='sell-iphone.php?cat=' + encodeURIComponent(val); } <?php @$cat=urldecode($_GET['cat']); // Use this line or below line if register_global is off if(strlen($cat) < 0){ // to check if $cat is numeric data or not. echo "Data Error"; exit; } ?> Link to comment https://forums.phpfreaks.com/topic/272926-passing-a-database-record-with-ampersand-through-javascript-url/#findComment-1405754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.