0

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'];
?>

4 Answers 4

2

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

Change to:

$("#list").load("selectcity.php?testvar=" + encodeURIComponent(testvar));
Sign up to request clarification or add additional context in comments.

3 Comments

Only 2 people answering actually read the question fully - congratulations on being one of them ;)
@MrCode. Is there any way to pass multiple variables instead of just one ?
@user3541562 you can add more using the format testvar=a&second=b&third=c, or pass an object like $(..).load('myurl', { testvar : 'a', second : 'b', third : 'c' })
0

Just try with:

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

Comments

0

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

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

Should work.

Comments

0

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

2 Comments

its working..but is there any way to pass multiple variables ??
see answer, extended on the original answer a bit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.