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
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....

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.