views:

95

answers:

3

Hi guys

Is there a way to check the data sent by a form to a PHP page return to the form page WITHOUT resetting the data sent and show a error?

The form has 20 fields and I need to check one of them on a bd. If it fails the user may be redirected to the form page with the form populated and displaying a error message on the field which is 'wrong'.

I would like any advice of a technique instead of populating each field using PHP.

UPDATE:

I do not want to use any solution that involves repopulate the fields by myself!!!

I want a solution that return to the form page populated with the previous values. I've tried something like js.history.back or window.back(). But the form returns empty...

A: 

Yeah, just check the forms beforehand, make sure you run the form HTML after the validation procedure, and if validation fails, reload the form with all of the prior information in the fields.

It might look something like this:

<?php if(isset($_POST['submit']){
    // validation code goes here with a trigger variable to
    // contain true or false depending on the outcome    
}

if($trigger==false){
    // load up your post data here    
} 

echo '
    <!-- form HTML goes here -->
';
dclowd9901
there's already an js validation beforehand...
Paulo Bueno
Never rely on javascript validation alone. A client can send all sorts of crazy stuff by disabling js or working around it.
keithjgrant
thats exacly why im using php to validate. the problem is if the validation fails on php i want to redirect the user to the form page populated with the values inserted by the user. but header(location) will reset the form...
Paulo Bueno
The validation is set on another page... anyway i ve tho repopulate myself...
Paulo Bueno
+2  A: 

In your form fields HTML, add posted values as field value.

e.g:

<input type='text' name='email' value='<?php echo $_POST['email']; ?>' />
Haris
im trying to avoid to do this on every field...
Paulo Bueno
It's not really avoidable unless you want to use Ajax and disallow submission w/o JavaScript altogether.
dclowd9901
This is absolutely the standard way of responding to invalid form submissions. However, you must remember to use `htmlspecialchars()` over the value being echoed into the HTML, otherwise you've got a cross-site-scripting security hole. (Note: use double quotes for attribute value delimiter, otherwise you have to also pass in the `ENT_QUOTES` option to `htmlspecialchars()` on each call.)
bobince
A: 

On your Form Validation page

<?php
$form_values = $_POST;
//Your Form Validation
//$form_validated = TRUE/FALSE;  // FALSE if there were errors

if ($form_validated) {$form_values = array();}
$_SESSION['form_values'] = $form_values; /* if your form validation page and form page is not on the same page and can also use the same technique for error messages */
?>

On your Form page for each input field.

<?php
$form_values = $_SESSION['form_values']; //if your form validation page and form page is not on the same page
unset($_SESSION['form_values']);

echo '<input type="text" name="user_name" value="'.$form_values['user_name'].'" />';
?>

Different input types will need to handle the $form_values array differently but this should get your started.

If you are not using a seperate form validation page, you can get the form values directly form the $_POST array.

bigstylee
Yes, i kwon i shou use something like $_session['any']=$_post to pass the array back... but I WANT TO AVOID TO DO THIS...
Paulo Bueno
If your form validation is on another page then you dont have many more options avialable to you unfortunately. Other solutions would include storing it in a cookie or a database but using the $_SESSION is going to be the easiest way to achieve this. I would recommend keep form validation and displaying the form on the same page. I used to have a seperate page for all form validation but this soon becam HUGE which made it almost impossible to maintain.
bigstylee