无法使用jquery帖子发布变量

时间:2013-05-03 01:11:33

标签: php javascript jquery post

我正在尝试将变量从模态表单传递到另一个页面。我在每个选择中使用id标签声明表单中的变量。

页面重新加载到test.php,但是没有变量可以回显。

的javascript

var id = $( "#id" ),
name = $( "#name" )

$.post("jqtest/test.php", { device_id: "id", device_name: "name" });
load('jqtest/test.php');

test.php的

echo $_POST['device_name'];
echo $_POST['device_id'];

2 个答案:

答案 0 :(得分:0)

var id = $( "#id" ), name = $( "#name" )
/*
if have tag like <input id='id' value='ABC'> and want to get ABC , $( "#id" ).val();

if have tag like <div id='id' >ABC</div> and want to get ABC,$( "#id" ).html();
*/
$.post("jqtest/test.php", { device_id: "id", device_name: "name" });
/*
after get id and name , need
$.post("jqtest/test.php", { device_id: id, device_name: name });
*/

load('jqtest/test.php');
/*
  useless delete this line
  if need callback, ref:
  $.post("test.php", { name: "John", time: "2pm" })
    .done(function(data) {
    alert("Data Loaded: " + data);
    });
*/

答案 1 :(得分:-1)

你没有调用load来获取帖子的内容!

$.post("jqtest/test.php", { device_id: "id", device_name: "name" }, function (data) {
    //handle the response here
    console.log(data);
});

除非你调用某种功能,否则使用这样的负载是错误的。


编辑,

根据您的评论,由于您正在加载新页面,因此回显不起作用,您没有处理Ajax请求中的后置代码。如果您想导航到表单提交的页面,请提交实际表单!您使用的是Ajax错误。