I have a form on my website, which has a series of boxes. When the submit button is clicked it checks to ensure that all of the sections are relevant using PHP. (IE if the email is an email, if the name doesn't have characters that are invalid etc.)
In the PHP script if this happens, it currently does:
function died($error) {
// your error code can go here
header( "refresh:0;url=construction.html" );
echo $error;
die();
Now it refreshes the page. So if you get one thing wrong, it will clear the form. Is there a way within PHP to get this instead to go back (Thus keeping the form that was filled in, filled in).
The website that it is currently on is here. I believe that it could be some implementation of JavaScript, but what this is and how to format it, eludes me.
Thanks for any h开发者_运维技巧elp!
What you are calling 'ensure if relevent' is called validation and is a normal and necessary step when submitting forms
You always need server side validation (in your case PHP), meaning you can add client side validation (Javascript) for the sake of usability and speed but you can never omit the server side validation because of security reasons
Feeding the valid data back to the form is a common practice too, because not doing it is user unfriendly
Your function died($error) looks like something weird and unnecessary, failed validation is no reason to die.
Sow what should you do?
Javascript (client side)
With Javascript, you can pre-validate your form to avoid unnecessary server roundtrips and give immediate feedback to the user. There are plenty of implementations with jQuery, dojo, MooTools, etc. for form validation. But don't forget, Javascript can be turned off, so you have to validate everything on the serverside too!
PHP (server side)
One good way would be to use an existing validation class like Zend_Validate or even Zend_Form. Zend_Form makes it very easy to feed back the validated post data back to the form with e.g. $form->populate($data)
.
Any other framework or library with form support will also help. Of course you don't need a library for that, so you will need to read about how to populate a form with the original valid post data. If you do that, make sure you can send a copy of the $_POST array back to the client, where you store the original values, if they were valid and a flag/message for the fields that were not valid. A basic way of integrating this into the markup would look something like this:
<input type="text" value="<?php echo ($validatedPost['email']['isValid']) ? $validatedPost['email']['value']: '' ?>" name="email" />
<?php if ($isPostBack && !$validatedPost['email']['isValid']) : ?>
<p class="invalid"><?php echo $validatedPost['email']['message']; ?></p>
<?php endif; ?>
The PHP-way of doing it would be something like the following (using sessions):
<?php
session_start();
function died($error) {
//Store the input
$_SESSION['temp_form'] = array(
'name' => $_POST['name'],
'email' => $_POST['email']
);
// your error code can go here
header( "refresh:0;url=construction.html" );
echo $error;
die();
}
?>
On construction.html
(you might want to make it a .php
if this is going to work, or make .html
bound to php:
<input type="text" name="name" value="<?php if( isset($_SESSION['temp_form']['name']) ) echo $_SESSION['temp_form']['name']; ?>" />
<input type="text" name="email" value="<?php if( isset($_SESSION['temp_form']['email']) ) echo $_SESSION['temp_form']['email']; ?>" />
Hope this makes your situation more understandable for you!
Edit:
Don't forget to add session_start();
up top in the construction.php
.
I would highly recommend you do form validation on the client side via JavaScript in addition to doing it server side via php.
Try jQuery Form Validation Plugin.
For this you'll need a basic understanding of jQuery
You should keep all the info in a session (or perhaps submit to the same page and omit the referesh?), and then when generating the form, check for it in fill it out from the data you have.
You can't do this as you described with PHP alone. You could "force" the browser to go back one page, but there's no guarantee that the browser would remember and keep the form field values.
The solution is to fill the fields with the values the user submitted if the validation fails. For example:
$email = '';
if( !empty( $_POST[ 'email' ] ) ) {
$email = $_POST[ 'email' ];
}
?>
<input type="text" value="<?php echo $email; ?>" name="email" />
<?php
if( /* email didn't validate */ ) {
echo 'Please give a valid email address';
}
精彩评论