Skip to content
Advertisement

How to execute different functions in same script by clicking different buttons

I have a button inside my form as below,

JavaScript

So my script as below, from that I will run me testme.php.

JavaScript

My testme.php as follows,

JavaScript

This works fine..Now I need to do this same approach to many buttons..only different is the button id and the runnning script only… As an example,

JavaScript

But I need to change the function from my testme.php as well as below,

JavaScript

Can I do this task by using only one script?

Advertisement

Answer

As a basic example of how you might accomplish the stated objective of triggering different pieces of PHP code dependent upon which button is clicked in the browser perhaps the following might be of help.

The javascript binds a click event handler to each button which sends the ID if the event.target (button ) within an ajax request.

The PHP script processes the $_POST['bttn'] variable and uses a simple switch to fork the program logic

JavaScript

As I do not use jQuery at all and cannot find reference to the .confirm method I can offer no advice on it. However using vanilla Javascript and some additional code it is possible to popup a dialog to serve your needs as I understand them.

The following snippet uses a class I wrote a while ago to interact with a HTML dialog element. The callbacks are assigned to the buttons and if the Accept button is clicked the callback will fire an ajax request with the button ID that invoked the dialog. ( use the console to inspect the network traffic )

JavaScript
JavaScript
JavaScript
Advertisement