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; } ?> Quote Link to comment Share on other sites More sharing options...
haku Posted January 10, 2013 Share Posted January 10, 2013 (edited) 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. Edited January 10, 2013 by haku Quote Link to comment Share on other sites More sharing options...
The Letter E Posted January 15, 2013 Share Posted January 15, 2013 (edited) 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; } ?> Edited January 15, 2013 by The Letter E Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.