How to Check if the Submit Button Is Clicked in PHP

php


In this article, we will go over how you can check to see whether the submit button of a form has been clicked or not using PHP.

This is very useful and can be used often with form data in PHP, because when the web browser first loads a web form, PHP does not know whether the form, initially, has been clicked or not. It doesn't know whether the user has entered data and submitted or not. This means that if you have a form field and it does an if-else statement, it may run one of the statements initially, which you probably don't want. PHP cannot distinguished if a form has already been executed or it hasn't, when initially loaded. So checking to see if the submit button has been clicked is important and useful in forms involving PHP.

Below is an typical form field that you would find on the web:





Result



Above is a form which asks a user to enter his or her name into the form field. If the user enters in characters into this field and clicks submit, then the form will tell the user the name which s/he entered. If the user leaves the field blank and clicks 'Submit', then the form will tell the user that s/he did not enter a name and that s/he must do so.

HTML Code

The HTML code of this form field is:



The most important part of this form field in HTML is the "name" of the submit button, which in this case is called "submitbutton". This will be important because our PHP code needs to extract information from this submit button, which is how it determines whether it has been clicked or not. PHP code knows which submit button to refer to by the "name" attribute of the submit button.

PHP code

The PHP code used to determine whether the submit button has been clicked is:



With the PHP Code, first we have to extract the data from the submit button before we can process it to check whether it is clicked or not. We do this by the line:

$submitbutton= $_POST['submitbutton'];

Here, we created a PHP variable, $submitbutton. Using the superglobal $_POST array, we extract the value of the submit button with the form name 'submitbutton', and assign it to the PHP variable, $submitbutton.

Now that we have the value extracted, we can do an if statement to see whether it is clicked or not. We do this by the following line:

if ($submitbutton){
//This statement to be executed
}
else
{
//This statement to be executed
}

How we check to see if the submit button is clicked is with a simple if statement. If the $submitbutton is clicked, the function will return true. If the $submitbutton is not clicked, the function will return false. This simple if statement checks the boolean value of the variable.

Based on this, you can make any function or statement be executed.

In our example, if the submit button has been clicked, the form tells the user the name s/he has entered. If it hasn't, then the form tells the user to enter a name.

If we didn't do validation to check whether the submit button was clicked, then initially when the form is first loaded, since the field is empty (not set), the form would automatically tell the user that s/he must enter a name. This is because PHP doesn't know at initial loading whether the user has interacted with the form already or not. The validation to check whether the submit button has been clicked demonstrates this.


Related Resources

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create a Search Engine Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Confirmation Page for an HTML Web Form Using PHP

How to Insert Images into a MySQL Database Using PHP

How to Create a Searchable Database Using MySQL and PHP

How to Create a Register and Login Page Using PHP




HTML Comment Box is loading comments...