I have a php file that contains a HTML form, then PHP logic, then an HTML footer…in that order.
I am trying to get the values from the form into some php validation logic in the botton of the page (but before the footer) using <?php VALIDATION LOGIC HERE ?>.
the problem is when the validation finds an error, the php logic will die() and hence, my HTML footer will not be executed.
is there a way to get my HTML footer to execute despite my php die();? …or is there a better overall way to integrate my HTML and PHP? Thanks! Anyhelp would be much appreciated.
EDIT:
I actually have die() almost everywhere in my code where I am about to connect to a database. If the user credentials are correct, they connect..if credentials are wrong then it will die()..
Is this good practice and use of die()? it seems the solution to my problem is to use return() INSTEAD OF die()…in order to continue processing the HTML footer.
Also, I have situations such as mysql_connect() or die(). How can i would continue processing the remaining HTML page when die() is executed before the HTML is processed? ..i don’t think mysql_connect() or return; is good practice right?
Thanks so much again in advance! The feedback has been very helpful!
As other states, you should have multiple files; header.php, index.php, footer.php, formvalidator.php.
In your index.php file, you should include header.php and footer.php.
In the form tag, action is sett to load formvalidator.php
In the form validator script, you could have something like this:
Now you can loop through the array and validate each field.
If you find an error, echo an error message, otherwise process the form.
Update
Answer to your update.
You should “never” use die(). Instead, exit thefunction you are in and return an error message. If you simply die(), you never know what went wrong where.
It is not possible to do server validation of a form unless you click the submit button.
You can put the code I gave you in the same PHP file as the form, and when you submit, you simply reload the same page (just set
action="<?= $_SERVER['PHP_SELF'] ?>")If you want to validate fields before submit, you must to this using javascript, like e.g. jQuery.validate.
Hmm… seem like you need some more knowledge of how to mix PHP with HTML.
Take a look at this beginners guide on how to work with forms in PHP.