Re: reloading forms and keeping variables.
| From: | Jesper Juhl | Date: | Fri, 28 Jul 2000 07:52:06 +0000 |
| Subject: | Re: reloading forms and keeping variables. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-8726@lists.php.net to get a copy of this message | ||
> I have a form that the user fills in which is then reloaded to check to
all
> the fields are filled correctly, if they are not, variables are changed
to
> the value of 1 which the page then picks up on and alerts the user to
fill
> in each field that isn't correct, however, the page does not keep the
values
> of the fields the user has filled in already, its as if the user just
loaded
> the page.
> Is there any way to keep these values when the page is reloaded ?
> Here is a shorten example of the form layout and code I am using.
> Mark = 2, once the user clicks submit, to trigger the page to reload
using
> $PHP_SELF and check for un entered fields like this one.
> if(!$Num_adults and $mark==2):
> $adult1=1;
> endif;
> <form action=\"$PHP_SELF\" method=\"post\">
> form goes here
> triggered error message
> if($adult1==1):
> echo"error message";
> endif;
> more form fields
> <input type=\"hidden\" name=\"mark\" value=\"2\">
> <submit>
> </form>
> Any help any one can give me with this would be greatly appreciated as I
> have ran out of ideas to keep the variables!!
> Many thanks
> Ade !
> ------------------------------------------------------
> ade@stormbreaker.com
The following piece of code should do what you want (you may want to keep
an eye open for missing quotes and the like, as StarOffice mangles my
mails in mysterious ways when posting to newsgroups) :-)
----------[ Cut here ]----------
<?php
If (!empty($submit)) {
/* do form processing */
echo <b>Validating the form</b><br>;
echo recieved the following variables : <br>;
/* loop through the input array and show values */
$arr = array_values($input);
for ($i = 0; $i < sizeof($arr); $i++) {
echo $arr[$i].<br>;
}
echo <hr>;
/* you can also use named keys */
echo field1 = .$input[field1].<br>;
echo field2 = .$input[field2].<br>;
}
/* now show the original form */
?>
<form method=post action=<?php echo $PHP_SELF; ?>>
<input type=text name=input[field1] value=<?php echo $input[field1];
?>>
<input type=text name=input[field2] value=<?php echo $input[field2];
?>>
<input type=submit name=submit value=Click me>
</form>
----------[ Cut here ]----------
Best regards,
Jesper Juhl
juhl@eisenstein.dk