You may have encounter website page types were should the user would make a error in filling the kind the error message would be shown (commonly in red) just previously mentioned the actual type command (area). I exhibit you the way to do this with PHP in this post.
Case in point HTML Kind
We shall make use of the subsequent HTML sort for illustration:
<html>
<head>
</head>
<body>
<form method=”post” action=”myPHP.php”>
<br
Name: <input type=”text” name=”Name”><br
<br
Email: <input type=”text” name=”Email”><br
<br
Message: <textarea cols=”50″ rows=”3″ name=”Message”></textarea><br <br
<button type=”submit”>Send</button>
</form>
</body>
</html>
Inside the kind the approach is Publish plus the action attribute has the identify on the PHP file, myPHP.php. This PHP file as well as the HTML file for that form are inside exact directory at the server.
Feedback Technique
Now the PHP file (script) includes a duplicate of your HTML Type website file code and it slots compact PHP code segments where error messages are expected inside the copy. For the HTML code over, a smaller PHP code segment might be slotted just above the tag for just a command inside copy. Every handle tag in the copy will have a specific PHP code segment above it, which is certainly accountable for its error concept.
With the commencing the consumer will contact the HTML kind in the browser. The fields will be empty. He then fills the sort and clicks the Send Button. The PHP system (myPHP.php) is called. The PHP program will validate the controls and when there are errors a little PHP code section above a control tag (duplicate) will send an error message again into the browser. If there may be no error, then the sole message sent back again can be which the form information and facts has long been efficiently obtained.
Now, if following suggestions may be sent with error message along with the person makes an error again, the PHP script will nevertheless be run for the reason that the copy of the internet form now on the browser however calls the identical PHP script from its action attribute. So, after the very first error may be dedicated through the consumer, the PHP script will retain calling alone, provided that the user keeps generating errors and clicking the Send button.
A simple PHP Program
There are actually 4 primary code segments from the straightforward PHP program (script) I will supply you with. It operates with all the previously mentioned HTML file. The initial of these code segments will get the values from the controls sent in the internet type. The next of these segments will validate the controls and if there may be any error, it’ll set certain flag variables. If there exists any error the 3rd of these segments will deliver a comprehensive copy with the website form with slotted error messages again to a new web page in the browser. If there’s no error, the fourth of these segments will send a confirmation (saying kind information successfully obtained) to a new web page on the browser; in this instance the copy of the website type will not be sent again.
The first Primary Code Segment
This is certainly the primary main code segment with the PHP script:
//obtain values from web form
$ NameVal = $ _POST['Name'];
$ EmailVal = $ _POST['Email'];
$ MsgVal = $ _POST['Message'];
There’s a world-wide predefined array termed $ _POST in PHP. If you send the HTML form details with the post strategy then this array will have the values in the controls sent. To have the appeal of every management, you need the name of your handle. You assign the return worth to a variable with any identify you’d like. The 2nd Primary Code Segment This is the 2nd primary code segment:
//error flags
$ errorOccurred;
$ nameError;
$ emailError;
$ msgError;
//simple validation
if ($ NameVal == “”)
{
$ errorOccurred = “true”;
$ nameError = “true”;
}
if ($ EmailVal == “”)
{
$ errorOccurred = “true”;
$ emailError = “true”;
}
if ($ MsgVal == “”)
{
$ errorOccurred = “true”;
$ msgError = “true”;
}
The initial 4 lines over are error flags (variables). If there is any error whatsoever, typed (triggered) from the user, then the £ errorOccurred variable could have the benefit, “true”. If there exists an error along with the user name, then $ nameError can have the price “true”. If there exists an error while using the electronic mail, then $ emailError could have the appeal “true”. If there’s an error with all the message subject then $ msgError could have the price, “true”. Following the above 4 lines, you’ve got four if-constructs.
These 4 constructs do some simple validations of your sent industry values. They basically check if a field worth was empty. You can make the validation more difficult than this if you need to, but I will not go into that in this article. If any area value is empty, then the generalized error variable, £ errorOccurred is granted the price, “true”; also the corresponding error variable is given the value, “true”. The values of these flag variables are going to be used to determine if suggestions error messages are to get sent again towards the browser.
The Third Principal Code Section It is the 3rd principal code section:
//display copy and error messages if error occurred
if ($ errorOccurred == “true”)
{
echo “<html>\n”;
echo “<head>\n”;
echo “</head>\n”;
echo “<body>\n”;
echo ” <form method=\”post\” action=\”myPHP.php\”>\n”;
if ($ nameError == “true”)
{
echo “<span style=\”color:red\”>The Name field cannot be empty!</span>”;
}
echo “<br \n”;
echo “Name: <input type=\”text\” name=\”Name\” value=\”" . $ NameVal . “\”><br \n”;
if ($ emailError == “true”)
{
echo “<span style=\”color:red\”>The Email field cannot be empty!</span>”;
}
echo “<br \n”;
echo “Email: <input type=\”text\” name=\”Email\” value=\”" . $ EmailVal . “\”><br \n”;
if ($ msgError == “true”)
{
echo “<span style=\”color:red\”>The Message field cannot be empty!</span>”;
}
echo “<br \n”;
echo “Message: <textarea cols=\”50\” rows=\”3\” name=\”Message\”>” . $ MsgVal . “</textarea><br <br \n”;
echo “<button type=\”submit\”>Send</button>\n”;
echo “</form>\n”;
echo “</body>\n”;
echo “</html>\n”;
}
All the code here is in a single major if-construct. The situation of your huge if-construct to begin with checks should the value with the generalized error variable (errorOccurred) is “true”. If it is actually legitimate then a duplicate on the online page has to be sent back again to the browser, with error massages slotted at suitable positions.
The PHP echo perform is made use of to deliver the lines in the copy from the internet web page.
Now, notice the little PHP code segments just over the tags of every management inside the duplicate. Each individual of them is surely an if-construct, which checks in the event the corresponding flag variable is “true”. If it really is true, then the block with the if-construct sends the error message.
Notice that whilst sending a copy of your regulate tags, the value from the manage, and that is what the person typed, is additionally sent; using this type of the person may not have to retype the area value.
So the third main code segment (block of substantial if-construct) operates only if there was not less than one particular error. If there was no error, then the third main code segment will not run; it is actually the fourth primary code section that can operate.
The Fourth Major Code Segment
That is the fourth primary code section:
//if no error occurred, send feedback that web info has been received
if ($ errorOccurred != “true”)
{
echo “<html>\n”;
echo “<head>\n”;
echo “</head>\n”;
echo “<body>\n”;
echo “<h1 style=\”color:darkblue\”>Thank you.</h1>\n”;
echo “<h2 style=\”color:darkblue\”>The Information has been received.</h2>\n”;
echo “</body>\n”;
echo “</html>\n”;
}
This section is one more significant if-construct. It checks if there was any (a minimum of 1) error utilizing the generalized error variable, errorOccurred. If there was no error, then it sends a confirmation message to a new page on the browser. Using this script you don’t need to have any particular assertion to deliver the tags to some new browser web page. In the event the script was known as from the browser, then just about every echo instruction will deliver its argument to the very same net page on the browser.
Remarks
In practice, for that net page, chances are you’ll want a heading on the top on the sort to point that an error has occurred, in a single with the controls, and after that the person can scroll down to see the actual management and its error. You could attain (code) that making use of the generalized error variable, errorOccurred. In the website web page copy while in the script an H3 factor along with the generalized error concept might be sent just under the get started tag on the sort.
Following the PHP script has systematically received the values with the sort controls, you’ll be able to do whichever you desire to accomplish along with the values. You can deliver them to a database; you are able to deliver them to a printer otherwise you can send them to an e mail box. Check with my article sequence regarding how to deliver email with PHP, titled, Sending Email with PHP.
Conclusion
To possess similar page feedback for a internet sort, the PHP script known as will need to have a copy with the website type web page. If you can find an error concept, the duplicate is sent back again together with the error concept slotted at a related position of your duplicate. The duplicate is in echo statements and not in isolation. The internet (HTML) sort is called after; soon after that if the person retains making mistakes, it’s the PHP script that may be termed (calling alone) around and above once again.

