[英]Pass value from jquery variable to php variable
I need to get id from table row by clicking on that row and use that id in another php file. 我需要通过单击表行来获取ID,并在另一个php文件中使用该ID。 For this I was getting id by jquery, the code is
为此,我通过jquery获取ID,代码为
var id = $(this).closest("tr").find('td:eq(0)').text();
// it's working //工作正常
And I want to pass this id as php variable. 而且我想将此ID作为php变量传递。 But any one isn't work.
但是任何人都行不通。 My code is:
我的代码是:
$(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);
}
});
});
});
And my php code is: 和我的PHP代码是:
$a = $_GET['id1'];
echo $a ;
Your selector is already a tr
element: 您的选择器已经是
tr
元素:
var id = $(this).find('td:eq(0)').text(); // use this
so you don't have to traverse up to the tr
* with .closest()
, you can remove it. 因此您不必通过
.closest()
遍历tr
*,可以将其删除。
and in the success function you are using a wrong argument: 在成功函数中,您使用了错误的参数:
success: function (result) {
alert(result); // <-------update to this
}
and at php
end you can use isset()
method: 在
php
端,您可以使用isset()
方法:
if(isset($_GET['id'])){
$a = $_GET['id1'];
echo $a ;
}
* depends on your dom structrue actually. *实际取决于您的dom结构。
Try jquery $.get
: 试试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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.