user3541562
user3541562

Reputation: 271

Passing Javascript variable to PHP page with url

I have a javascript function that loads a PHP page and passes a variable(testvar) to it

function selectcity()
{

      var testvar = "test text";
     $("#list").load("selectcity.php?testvar");
     $.ajaxSetup({ cache: false });

}

Now in the selectcity.php page,I am trying to retrieve the variable like this : But not working,Can someone pls help.

<?php
echo $_GET['testvar'];
?>

Upvotes: 0

Views: 30

Answers (4)

patrick
patrick

Reputation: 11711

you need to set a value there as well...

$("#list").load("selectcity.php?testvar=true");

right now "testvar" is empty, so echoing it will echo an empty string... hard to see an empty string that way...

If you want the string 'testvar' to show in the URL use this:

$("#list").load("selectcity.php?testvar="+testvar);

You can also pass parameters like this:

$("#list").load("selectcity.php",{
   testvar: testvar,
   moreparam1: "some value",
   evenmore: 124
});

Upvotes: 0

Thargor
Thargor

Reputation: 1872

Because you set the variable "testvar" but do not put any thing in it.

$("#list").load("selectcity.php?testvar=foobar");

Should work.

Upvotes: 0

hsz
hsz

Reputation: 152206

Just try with:

$("#list").load("selectcity.php", {
    testvar: testvar
});

Upvotes: 0

MrCode
MrCode

Reputation: 64526

You didn't set the value, only the variable name in the URL.

Change to:

$("#list").load("selectcity.php?testvar=" + encodeURIComponent(testvar));

Upvotes: 2

Related Questions