Jump to content

Passing a database record with ampersand through javascript url


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.

Edited by haku

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 by The Letter E
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.