everisk Posted April 13, 2009 Share Posted April 13, 2009 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 More sharing options...
ToonMariner Posted April 13, 2009 Share Posted April 13, 2009 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 https://forums.phpfreaks.com/topic/153850-keep-form-value-after-submit/#findComment-808557 Share on other sites More sharing options...
everisk Posted April 13, 2009 Author Share Posted April 13, 2009 Thank you. Unfortunately I dont post to the same script but I guess I can use the same approach. Thanks! Link to comment https://forums.phpfreaks.com/topic/153850-keep-form-value-after-submit/#findComment-808611 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.