Jump to content

Passing a database record with ampersand through javascript url


frshjb373

Recommended Posts

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;
}
?>

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.

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;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.