0

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 ;
1
  • update alert(id) with alert(result); Commented Dec 9, 2015 at 7:41

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.