Jump to content

Archived

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

mark_nsx

how to retain contents of a form field

Recommended Posts

how do i retain the contents of all the fields in a form after the form has been
filled incorrectly and submitted? thanks!

Share this post


Link to post
Share on other sites
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:

<?php

if ( ( $_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:

<?php

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

<?php

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

Share this post


Link to post
Share on other sites
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:

<?php

if ( ( $_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:

<?php

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

<?php

if ( 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!

Share this post


Link to post
Share on other sites

×

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.