|
Posted by Umberto Salsi on 11/30/98 11:51
Ørjan Langbakk <pubblicc@start.no> wrote:
> See, I have, first, a check for existing email - if there is no email
> present, I want it to check if there is a phonenumber entered, and if
> _both_ those conditions are false, then I want to redirect.
Try this scheme:
$err = ""; # here we collect all the error msgs
if( invalid email )
$err .= "<li>Invalid email.";
if( invalid xxxx )
$err .= "<li>Invalid xxxx.";
if( invalid yyyy )
$err .= "<li>Invalid yyyy.";
/* ...and so on for the other fields to be validated */
Now we check the resulting error message $err:
if( length($err) > 0 ){
echo "<html><body><h1>Error</h1><ul>$err</ul></body></html>";
exit();
}
....here save the data or display the results...
or:
if( length($err) > 0 ){
redirect to http://www.xyz.xx/somewhere.php?err=" . rawurlencode($err)
so that the page somewhere.php can display the reason of the failure
exit();
}
....here save the data or display the results...
But the simplest, better way to handle the input/validation/error cycle
is using a single PHP page:
<?php
class MyForm {
public $field1 = "";
public $field2 = "";
/* ... */
}
function myform_display(MyForm $f = NULL, $err = "")
{
if( $f === NULL ){
# request to display an empty FORM:
$f = new MyForm();
}
echo "<html><body>";
if( length($err) > 0 )
echo "<ul>$err</ul>";
echo "<form method=post action='", $_SERVER['PHP_SELF'], "'>";
echo "<input type=text name=field1 value=\"",
htmlspecialchar($f->field1), "\">";
/* ...and so on for the other fields */
echo "<input type=hidden name=form_name value=myform>";
echo "<input type=submit name=b value=OK></form></body></html>";
}
function myform_validate()
{
/* Acquires the POSTed data: */
$f = new MyForm();
$f->field1 = (string) $_POST['field1'];
$f->field2 = (string) $_POST['field2'];
/* .... */
/* Validation: */
$err = "";
if( invalid $f->field1 )
$err .= "<li>Invalid xxxxxxxxxxxx";
if( invalid $f->field2 )
$err .= "<li>Invalid yyyyyyyyyyyy";
/* ....... */
if( length($err) > 0 ){
myform_display($f, $err);
exit(0);
}
/* Save or otherwise use the resulting, valid data $f: */
...save the data...
...redirect to the next page...
}
# main()
if( isset($_POST) && isset($_POST['form_name'])
&& $_POST['form_name'] === 'myform' ){
# that's just a POST from this FORM:
myform_validate();
} else {
# displays an empty FORM:
myform_display(NULL, "");
}
?>
Best regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it
[Back to original message]
|