Re: Question about form submitting
| From: | Philip Thompson | Date: | Tue, 03 Apr 2007 21:55:06 +0000 |
| Subject: | Re: Question about form submitting | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252013@lists.php.net to get a copy of this message | ||
On Apr 3, 2007, at 1:13 PM, Mário Gamito wrote:
Hi, Sorry for the lame question, but i didn't find a satisfactory answer in the web. I have this subscribe form (subscribe.php) and on submit i have to check for errors: a) password and password confirmation mismatch; b) missing filled fields c) check e-mail validity d) etc. My question is how do i make all these possibilities show a different error message without leaving subscribe.php ? I know that the for action must be subscribe.php, from there i'm blind as a bat. Any help would be appreciated. Warm Regards --:wq! Mário Gamito--------------------------------- <?php // subscribe.php session_start(); if (isset ($_POST)) { if (error condition 1) $_SESSION['error']['field1'] = $error = true; if (error condition 2) $_SESSION['error']['field2'] = $error = true; if (error condition 3) $_SESSION['error']['field3'] = $error = true; if ($error)
// Using SESSION variables
$_SESSION['field1'] = $_POST['field1'];
$_SESSION['field2'] = $_POST['field3'];
$_SESSION['field3'] = $_POST['field3'];
header ("location: subscribe.php?error=someError");
exit;
}
// OR using GET variables
if (error in form) {
$list = "&field1=".urlencode($_POST['field1'])."&field2=".
urlencode($_POST['field2'])."&field3=".urlencode($_POST['field3']);
header ("location: subscribe.php?error=someError$list");
exit;
}
} else if ($_GET['error']) {
// SESSION
$field1 = $_SESSION['field1'];
$field2 = $_SESSION['field2'];
$field3 = $_SESSION['field3'];
// GET
$field1 = urldecode($_GET['field1']);
$field2 = urldecode($_GET['field2']);
$field3 = urldecode($_GET['field3']);
}
?>
<html>
...
<form action="subscribe.php" method="post">
Field 1: <input type="text" name="field1" value="<? echo $field1; ?" />Field 2: <input type="text" name="field2" value="<? echo $field2; ?<? if ($_SESSION['error']['field1']) echo "Error 1!"; ?>
" />Field 3: <input type="text" name="field3" value="<? echo $field3; ?<? if ($_SESSION['error']['field2']) echo "Error 2!"; ?>
" />... </form> ... </html> <? // Be sure to destroy the error fields so that they don't show up next submit unset ($_SESSION['error']); ?> --------------------------------- That's one way to specify an error for each field. It's tedious, but forms are. Hope that helps. ~Philip<? if ($_SESSION['error']['field3']) echo "Error 3!"; ?>