Reputation: 271
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
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
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
Reputation: 152206
Just try with:
$("#list").load("selectcity.php", {
testvar: testvar
});
Upvotes: 0
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