mark_nsx Posted March 7, 2006 Share Posted March 7, 2006 how do i retain the contents of all the fields in a form after the form has beenfilled incorrectly and submitted? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/4319-how-to-retain-contents-of-a-form-field/ Share on other sites More sharing options...
txmedic03 Posted March 7, 2006 Share Posted March 7, 2006 Well I would need more information about the form to give you an exact script to demonstrate how to cover your particular issue, but simply put after you process the form for errors there are two ways that spring to mind.Number one is if the form is on form.php and it is also the action, that is the form.php file also processes the form. Then you can use your $_POST array.<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <input type="text" name="field1" id="field1" value="<?php echo $_POST['field1']; ?>" /> <input type="submit" value="Test" /></form>Let's just say for simplicity of this example that you want to make sure that whatever was entered into the form was at least 5 characters long. If this were the case you could place this PHP block above your form:<?phpif ( ( $_SERVER['REQUEST_METHOD'] == "POST" ) && ( strlen($_POST['field1']) < 5 ) ) { echo "<p>ERROR! field1 must be at least 5 characters.</p>\r\n";}?>This simply checks to make sure your form has been submitted and if field1 is less than 5 characters long it tells the user they messed up. Then your form will contain the previous user input.You may want to not display the form if they get everything correct or simply redirect to another page if they get everything correct. You can use the if statement to stop the form from being displayed or a head() to redirect on success.Now I meantioned that two ways sprank to mind earlier. The second way is with two separate files where form.php is your form and process.php is the file that actually processes the user's input.Your form.php would be something like this:<form action="process.php" method="POST"> <input type="text" name="field1" id="field1" value="<?php echo $_GET['field1']; ?>" /> <input type="submit" value="Test" /></form>Again we will assume that field1 is too short for this example.Now with a redirection on error back to the form you can do this:<?phpif ( strlen($_POST['field1'] < 5 ) { head("Location: form.php?field1=".$_POST['field1']);} else { echo "Hey, good job!\r\n";}?>If you don't want to just use a redirection you could give the user a link to go back to the form.<?phpif ( strlen($_POST['field1'] < 5 ) die("<p>You didn't put enough characters in field1.<br />Please go back and <a href=\"form.php?field1=".$_POST['field1']."\">try again</a>.</p>");echo "Success!";?>In these last two examples I assumed that the page would not be viewed without having the form post to it, but if you wish you can use the REQUEST_METHOD from the first example for good measure. In this last example I used a die statement rather than if then else format. This is perfectly acceptable as is the if then else format. It will come down to a matter of personal preference. Quote Link to comment https://forums.phpfreaks.com/topic/4319-how-to-retain-contents-of-a-form-field/#findComment-15018 Share on other sites More sharing options...
txmedic03 Posted March 7, 2006 Share Posted March 7, 2006 Well I would need more information about the form to give you an exact script to demonstrate how to cover your particular issue, but simply put after you process the form for errors there are two ways that spring to mind.Number one is if the form is on form.php and it is also the action, that is the form.php file also processes the form. Then you can use your $_POST array.<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <input type="text" name="field1" id="field1" value="<?php echo $_POST['field1']; ?>" /> <input type="submit" value="Test" /></form>Let's just say for simplicity of this example that you want to make sure that whatever was entered into the form was at least 5 characters long. If this were the case you could place this PHP block above your form:<?phpif ( ( $_SERVER['REQUEST_METHOD'] == "POST" ) && ( strlen($_POST['field1']) < 5 ) ) { echo "<p>ERROR! field1 must be at least 5 characters.</p>\r\n";}?>This simply checks to make sure your form has been submitted and if field1 is less than 5 characters long it tells the user they messed up. Then your form will contain the previous user input.You may want to not display the form if they get everything correct or simply redirect to another page if they get everything correct. You can use the if statement to stop the form from being displayed or a head() to redirect on success.Now I meantioned that two ways sprank to mind earlier. The second way is with two separate files where form.php is your form and process.php is the file that actually processes the user's input.Your form.php would be something like this:<form action="process.php" method="POST"> <input type="text" name="field1" id="field1" value="<?php echo $_GET['field1']; ?>" /> <input type="submit" value="Test" /></form>Again we will assume that field1 is too short for this example.Now with a redirection on error back to the form you can do this:<?phpif ( strlen($_POST['field1'] < 5 ) { head("Location: form.php?field1=".$_POST['field1']);} else { echo "Hey, good job!\r\n";}?>If you don't want to just use a redirection you could give the user a link to go back to the form.<?phpif ( strlen($_POST['field1'] < 5 ) die("<p>You didn't put enough characters in field1.<br />Please go back and <a href=\"form.php?field1=".$_POST['field1']."\">try again</a>.</p>");echo "Success!";?>In these last two examples I assumed that the page would not be viewed without having the form post to it, but if you wish you can use the REQUEST_METHOD from the first example for good measure. In this last example I used a die statement rather than if then else format. This is perfectly acceptable as is the if then else format. It will come down to a matter of personal preference.Happy coding! Quote Link to comment https://forums.phpfreaks.com/topic/4319-how-to-retain-contents-of-a-form-field/#findComment-15019 Share on other sites More sharing options...
mark_nsx Posted March 7, 2006 Author Share Posted March 7, 2006 thanks very much txmedic03, i got it all sorted out! Quote Link to comment https://forums.phpfreaks.com/topic/4319-how-to-retain-contents-of-a-form-field/#findComment-15046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.