Jordan
Jordan

Reputation: 23

PHP Put form data back into form after form validation

I have a contact form which it's action is contact.php. Contact.php has all the form validation. Index.php has the form. When I click send on the form and one of the input tags are invalid it sends me back and tells me which input tags were incorrectly filled. The problem is all the other input values that were correct are now empty. Is there a way to save the original values of the input tags with out using $_GET? I would use $_GET, but I have a message textarea which can go up to 1000 characters. And I don't want 1000 characters in the URL.

Upvotes: 1

Views: 299

Answers (3)

Dan
Dan

Reputation: 9488

Why is your validation taking place in a function for a file that does not contain the form? Your validation should take place in index.php and then you can use $_POST. If all goes through then redirect the user to the next page.

<input type="text" name="my_input" value="<?=isset($_POST['my_input']) ? $_POST['my_input'] : NULL?>

The above uses a ternary operator and says that the value of the input is either going to be the $_POST value for this input ($_POST['my_input']) or it will be NULL. What determines whether it will be one or the other is whether isset($_POST['my_input'] is true or not.

Upvotes: 0

Erlesand
Erlesand

Reputation: 1535

You can either use a session variable, so in contact.php you could set for example $_SESSION['form_data'] = $_POST;, which you then could access from index.php.

Or you could use for example a jQuery validator to validate the text before the form was submitted.

Upvotes: 1

MrYellow
MrYellow

Reputation: 408

single-page.php

<?php

if ($_POST['action'] === 'doit') {
    // Validation and other logic
    // Send email using $_POST['email'] etc.
}

?>

<form>
<input type="text" name="email" value="<?php echo $_POST['email'] ?>" />
<input type="hidden" name="action" value="doit" />
<input type="submit" value="Contact" />
</form>

Upvotes: 0

Related Questions