Jump to content

Keep form value after submit


everisk

Recommended Posts

Hi,

 

I have a script where after form is submitted a series of check is done with PHP ( has to work with database so cannot check with javascript). If PHP found an error it should redirect back to the form and still keep the user entered value. How do I do that? My plan is just redirecting back with header:location but with this method, form value just got lost.

 

Any help would be greatly appreciated.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/153850-keep-form-value-after-submit/
Share on other sites

if you post back to the same script then its straight forward..

 

markup

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<?php echo $username;?>" />
<label for="password">Password</label>
<input type="password" name="password" id="password" />
<input type="submit" name="login_bttn" id="login_bttn" value="Login" />
</form>

 

the code that must come before...

<?php
$username = isset($_POST['username']) ? $_POST['username'] : null;
....
?>

 

this means that when the form shows then $username has been initialized and will not error. if the form had been submitted and validation failed so we show the form again then $username will be set to what was previously submitted....

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.