使用url将Javascript变量传递给PHP页面

时间:2014-04-16 14:10:35

标签: javascript php

我有一个加载PHP页面并将变量(testvar)传递给它的javascript函数

function selectcity()
{

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

}

现在在selectcity.php页面中,我试图像这样检索变量: 但不行,有人可以帮忙。

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

4 个答案:

答案 0 :(得分:2)

您没有设置值,只设置了URL中的变量名称。

更改为:

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

答案 1 :(得分:0)

试试:

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

答案 2 :(得分:0)

你需要在那里设置一个值......

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

现在“testvar”是空的,所以回显它会回显一个空字符串......很难看到一个空字符串......

如果您希望在URL中显示字符串'testvar',请使用以下命令:

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

你也可以传递这样的参数:

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

答案 3 :(得分:0)

因为你设置了变量“testvar”但是没有放任何东西。

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

应该工作。