将值从jquery变量传递给php变量

时间:2015-12-09 07:21:22

标签: php jquery variables

我需要通过单击该行从表行获取id并在另一个php文件中使用该id。为此我得到了jquery的id,代码是

var id = $(this).closest("tr").find('td:eq(0)').text(); //正在运作

我希望将此id作为php变量传递。但任何人都不行。 我的代码是:

$(document).ready(function(){
  $("#people-table tr").click(function(){
    var id = $(this).closest("tr").find('td:eq(0)').text();
     $.ajax({
        url: "default_people.php",
        type: "GET",

        data: { id1: id},
        success: function (result) {
                alert(id);
        }
    });
  });
});

我的PHP代码是:

  $a = $_GET['id1'];
  echo $a ;

2 个答案:

答案 0 :(得分:1)

您的选择器已经是tr元素:

var id = $(this).find('td:eq(0)').text(); // use this

因此您不必使用tr遍历.closest() *,您可以将其删除。

在成功函数中你使用了错误的参数:

success: function (result) {
     alert(result); // <-------update to this
}

php结束时,您可以使用isset()方法:

if(isset($_GET['id'])){
  $a = $_GET['id1'];
  echo $a ;
}

*实际上取决于你的dom structrue。

答案 1 :(得分:0)

尝试jquery $.get

$(document).ready(function(){
  $("#people-table tr").click(function(){
    var id = $(this).closest("tr").find('td:eq(0)').text();
     $.get("default_people.php?id="+id);
  });
});
相关问题