Jump to content

how to retain contents of a form field


mark_nsx

Recommended Posts

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.
Link to comment
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!
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.