0

I have 2 buttons "removeD" and "updateRecord"(by id) and I have written same ajax for them as follows:

$.ajax({
    url: 'DB/tableDisplay.php',
    type: 'POST',
    data: 'id='+uid,
    dataType: 'html'
})

But in tableDisplay.php I want to have different functionality for both the buttons.How to check the id of the button clicked in php? I've tried using : if(isset($_POST['removeD'])){ }else{ } But this is not working.

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94

4 Answers4

1

Try this:

$(document).ready(function(){

  $('button').click(function(){

     var id = $(this).attr('id');

     $.ajax({
        url: 'DB/tableDisplay.php',
        type: 'POST',
        data: {id: id},
        dataType: 'html'
      })

  });

 });
Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
vaibhav raychura
  • 162
  • 1
  • 10
0

Try the following code to detect the button clicked:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $('.click-button').on('click',function () {
            var button_id = $(this).attr('id');
            if(button_id == 'removeD'){
                alert('removeD button clicked');
            }else if(button_id == 'updateRecord'){
                alert('updateRecord button clicked');
            }

        });


    });
</script>

</head>

<input type="button" class="click-button" id="removeD" value="removeD">

<input type="button" class="click-button" id="updateRecord" value="updateRecord">
mith
  • 1,630
  • 1
  • 10
  • 12
0
        $('body').on('click','#id1',function(){
          $.ajax({
             url : 'url',
             data : {variable:values},
             type : 'html',
             dataType : 'GET/POST',
             success : function(data){
               console.log('Message after Success');
    },
             error : function(){
               console.log('Error Message')
    }

});
});

In your url page you can find whether ajax request is posted or not

    if(isset($_REQUEST['variable'])){
        write your php code here........
}
Dani
  • 875
  • 6
  • 13
0

You can use target it return which DOM element triggered the event. see below code its too easy and short to get id of clicked element.

 $('button').click(function(e){
      alert(e.target.id);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="remove">Remove</button><br>
<button id="update">Update</button><br>
Bharat
  • 2,443
  • 3
  • 23
  • 35