|
Posted by Mike on 05/25/07 07:40
On May 24, 8:02 pm, "J.O. Aho" <u...@example.net> wrote:
> damezumari wrote:
> > Tom, when I try your suggestion $_POST['userfirstname'] is empty. It
> > has a value in b.php, but I don't get any value when I try to use it
> > in a.php.
>
> > What am I doing wrong?
>
> > You used $_FORM, did you mean $_POST? If not, what is $_FORM?
>
> There isn't any $_FORM global variable in PHP.
> Depending on how you redirect the person back to page a, you either use $_POST
> or $_GET, when you don't know which method will be used, then use the
> $_REQUEST global
>
> Another method is to store values in a session, this way you don't have to
> send anything with the redirect.
>
> It's quite obvious that Tom made a typo, at least in my opinion, he meant
> $_POST in both cases.
>
> --
>
> //Aho
I do the check for valid inputs on the same page as the form. I have
a hidden value when the form is submitted.
When you first go to the page, the user hasn't submitted the form so
the hidden value isn't set so it will ignore all the checks. As Tom
suggests, you default the inputs with the $_POST values.
Once the user submits the form, the page calls itself with the hidden
value set. The same page can then check that all the inputs are
correct. If not, show the error. If they are, then either do what
you have to with the data or locate to a different page and do it.
Try this as an example....
<?php
if ($_POST[firstseen] == "no") {
//check inputs
if ($_POST[name] == "") {
$name_message = "* - You must enter a name";
$allok = "no";
}
if ($_POST[location] == "") {
$location_message = "* - You must enter a location";
$allok = "no";
}
if ($_POST[message] == "") {
$message_message = "* - You must enter a message";
$allok = "no";
}
if ($allok != "no") {
//re-direct to thankyou page
header("location: thanks.php");
}
}
?>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="post"
name="visitors" id="visitors">
Name<input name="name" value="<?php echo
stripslashes($_POST[name]); ?>" type="text" id="name" size="50"
maxlength="50<?php echo $name_message ?>
Location<input name="location" type="text" value="<?
php echo stripslashes($_POST[location]); ?>" id="location" size="50"
maxlength="50<?php echo $location_message ?>
Message <textarea name="message" cols="38" rows="7"
id="message"><?php echo stripslashes($_POST[message]); ?></textarea<?
php echo $message_message ?>
<input name="firstseen" type="hidden" value="no">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
Cheers
Mike
Navigation:
[Reply to this message]
|