I’m just wondering if anyone can give me an idea on how to do this:
I have a contact for that when submitted it gets send to a process_form.php file. In here I have some validation and then if it passes validation some action to perform.
I’m just wondering if its possible so that when i hit submit and my process_form.php hits an error, say for example incorrect password, how would you go about redirecting to the form page, and displaying an error on that page.
Validation is generally handled better by Javascript (except of course checking for ‘already exists’, which would be an AJAX and/or PHP implementation).
That said, doing it in PHP…
<?php
$errors = array();
//Foreach Form Field, Validate. If Error is found...
$errors[] = "Field Blah-de-blah Was Not Set";
//When you're done validating...
if(count($errors) > 0) {
echo "You did this wrong:<ul>";
foreach($errors AS $error) {
echo "<li>".$error."</li>";
}
echo "</ul>";
include_once('form.php');
die();
}
?>
Well I am also using javascript to validate however on just the browser side of things is bad practice, so I’m using php for server side validation(I want to make sure nothing bad gets into my database).
On submission of my form I’m sending the user to a process_form.php file. Do you know of a way to return back to the form.php if there is an error in the process_form.php?
If I’m not mistaken your way seems to check for errors and then includes a form?
Actually, handling validation with JavaScript alone is extremely risky. Validation should always be in place server-side. There’s no guarantee clients will have JavaScript enabled, and it’s simple to throw arbitrary data at scripts accepting POST and/or GET requests.
(except of course checking for ‘already exists’, which would be an AJAX and/or PHP implementation).
That said, doing it in PHP…
The introduction of AJAX, in this case, seems an unnecessary complexity. IMO, that’s more of a UI decision, which isn’t quite relevant to the OP’s question.
@Peck3277: you can always have the form submit to the same page. In your code, you check if the form has been submitted, then do further validation. If you’ve separated your code logic from presentation, you can easily use the results of the validation to set errors, if necessary, for the display of the document.
But if you really must redirect (the approach below is a bit ugly):
Hi dyer, thanks for the reply. I had thought of putting the codein the same page but I thought it would have been bad practice(I’m fairly new to php so I’m not sure if it is). I suppose I would be best off making my process_form.php into an external function and including it in the head?
In php form processing do people generally keep the code in the same page as the form?
It’s not necessarily bad practice. If you’ll refer back to StartLion’s post, you’ll notice how the form is included. You can make both your validation routines and the form HTML external to your “main” script, and simply include the form as necessary. In the form markup page (if it’s a PHP script), you can output an error string wherever you need. Depending on the logic preceding the inclusion, it can yield an error string or, perhaps, an empty string. I suggest playing around to see what works best for you.
In php form processing do people generally keep the code in the same page as the form?
Nothing is set in stone, it depends on what’s clearest and makes the most sense.