[英]Pass value from jquery variable to php variable
我需要通過單擊表行來獲取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 ;
您的選擇器已經是tr
元素:
var id = $(this).find('td:eq(0)').text(); // use this
因此您不必通過.closest()
遍歷tr
*,可以將其刪除。
在成功函數中,您使用了錯誤的參數:
success: function (result) {
alert(result); // <-------update to this
}
在php
端,您可以使用isset()
方法:
if(isset($_GET['id'])){
$a = $_GET['id1'];
echo $a ;
}
*實際取決於您的dom結構。
試試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.