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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
starfish
  • 13
  • 1
  • 4

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

  });

 });
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
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,680
  • 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
  • 905
  • 7
  • 14
  • In the above snippet you posted:if(isset($_REQUEST['variable'])){ write your php code here........ } . What should I write in 'variable'? I wrote the the "id" of the button there, but it's not working. Please help. – starfish Jan 25 '17 at 06:09
  • @PranaliNinawe did you checked others answer? – Alive to die - Anant Jan 25 '17 at 10:42
  • in variable you can pass any thing on which you can check whether you want to delete , update or add the record so that you can write code accordingly – Dani Jan 25 '17 at 11:25
  • you can also pass multiple variables like data : {id:id,name:name} etc – Dani Jan 25 '17 at 11:30
  • Thank you so much ! It was really helpful ! – starfish Jan 26 '17 at 14:48
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,441
  • 3
  • 24
  • 36