phpmysqlajaxbutton

How does server know which button was clicked


I’m having a problem I have a website that needs the function for users to accept the content or not. I have the datas in the Database (id, value, status) and I show them on a page. I use table - each row in the Database is one row on the table. At every row, I want to insert two buttons - Accept and Not Accept. The thing is how should I code for the server to recognize which button was clicked?! I use the Ajax to handle functions.


Solution

  • I mostly add data attributes to the buttons like data-id="1" data-action="accept" for the accept button and data-id="1" data-action="discline" for the not accept button.

    Give all buttons the same class name and add a click event to it with javascript. within the click event of your button read the attributes and add them to your ajax post request your server will have the row id then and the clicked action