Paul Est
Paul Est

Reputation: 23

return to previous page with form complet

Basically I have a registration form and when I click the submit button check if the email is valid.

However if there is a record of an error I do "header (" location: ". $ _SERVER ['HTTP_REFERER']);" to go to previous page. When this happens you need to write it all again in the form, how can I make the data to be filled?

Upvotes: 0

Views: 1165

Answers (3)

Dave
Dave

Reputation: 143

when you draw your form, you just need to check if the values are present.

$name = '';
if(!empty($_POST['name']){
    $name =  $_POST['name'];
}

<input type="text" name="name" value="<?php echo $name?>" />

Note: you should urlencode these values when applicable to curb xss attacks.

Upvotes: 0

H Hatfield
H Hatfield

Reputation: 856

You don't need to do a redirect if you are submitting to the same page that shows the form. Then you set the value of the form elements with the data passed in from the post and reshow the form. Your data will need to be escaped properly to prevent cross-site scripting attacks, etc.

Upvotes: 0

Eric Petroelje
Eric Petroelje

Reputation: 60498

You would need to temporarily store the previously entered values in the session:

$_SESSION['prevPost'] = $_POST;

Then you can use them to re-populate the form when the original page is loaded:

<?php
    $prevPost = $_SESSION['prevPost'];
?>
<input type="text" name="email" value="<?php echo $prevPost['email']; ?>"/>

Upvotes: 1

Related Questions