I need to get id from table row by clicking on that row and use that id in another php file. For this I was getting id by jquery, the code is
var id = $(this).closest("tr").find('td:eq(0)').text();
// it's working
And I want to pass this id as php variable. 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:
$a = $_GET['id1'];
echo $a ;
Try 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);
});
});
Your selector is already a tr
element:
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.
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:
if(isset($_GET['id'])){
$a = $_GET['id1'];
echo $a ;
}
* depends on your dom structrue actually.