How to detect which button was clicked and which button submits form data in PHP

detect-which-button-clicked-php

This is will a PHP basics posts. I see this question is asked a lot by the beginners so here it goes. Let’s see how you can detect which button was clicked in a PHP form.

Detecting Which Button was Clicked in PHP Forms

I am assuming you already know that for a button to be able to submit form data we need to place this button inside the form that we want to post (unless we are using JavaScript).

So, to find out the button that was clicked to submit a PHP form we need to give it name. We will use this name to check (on the server side) if a button was clicked or to detect which button was clicked. See the sample form code below:

Here when we click the “btnSubmit” button, form data will be posted to myPage.php page.

In myPage.php file I have a code block like below where first I will check if data was posted to this page and then I will check to see if “btnSubmit” button was clicked:

So the method we use to check which button is clicked to submit form data (or to check whether a button is clicked) is isset as show above. Please observe and see how I use it.

Hope this helps.
Good Luck,
Serdar

Leave a Reply

Your email address will not be published. Required fields are marked *