Forum Moderators: coopster
Chris
<?php
$true=$HTTP_GET_VARS['true'];
if($true==0){
#Generate initial prompting form
$month=date("n");$day=date("j");$year=date("y");$date="$month/$day/$year";
print " <form method=\"POST\" action=\"$PHP_SELF?true=submission\">
<p style=\"word-spacing: 0; line-height: 100%; margin-top: 0; margin-bottom: 0\">Date: <input type=\"text\" name=\"date\" size=\"20\" value=\"$date\"></p>
<p style=\"word-spacing: 0; line-height: 100%; margin-top: 0; margin-bottom: 0\"><input type=\"text\" name=\"miles\" size=\"20\" value=\"Enter Miles\"></p>
<p style=\"word-spacing: 0; line-height: 100%; margin-top: 0; margin-bottom: 0\"><input type=\"text\" name=\"location\" size=\"20\" value=\"Enter Location\"></p>
<p style=\"word-spacing: 0; line-height: 100%; margin-top: 0; margin-bottom: 0\"><select size=\"1\" name=\"time\">
<option selected>AM</option>
<option>Noon</option>
<option>PM</option>
</select></p>
<p style=\"word-spacing: 0; line-height: 100%; margin-top: 0; margin-bottom: 0\"><textarea rows=\"4\" name=\"comments\" cols=\"20\">Enter Comments</textarea><input type=\"submit\" value=\"Submit\" name=\"B1\"><input type=\"reset\" value=\"Reset\" name=\"B2\"></p>
</form>\n ";}
if($true==submission){echo 'blah';}
?>
if(!$_POST['B1'] ){
/* form code here */
} else {
echo 'blah';
}
if(!$_POST['B1'] &&!$_GET['true'] ){
/* form code here */
} else {
if( $_GET['true'] ){
$loc = $_SERVER['PHP_SELF'] . "?true=submission";
header( "Location: $loc" );
}
}
Here is a sample of how it can be used:
<?php
$errors=array();
if ($_POST['action']=='process'){
// validate
if (strlen($_POST['myvar'])<1){
$errors['myvar']="<b>myvar isn't set!</b><BR>";
}
// etc
}if (count($errors)==0){
// process the form
}else{
// show the form again, with errors indicated
?>
<form action="<?php print($PHP_SELF)?>" method="POST">
<input type="hidden" name="action" value="process">
<?php print($errors['myvar'])?>
<input type="text" name="myvar" value="<?php print($_POST['myvar'])?>">
<input type="submit">
</form>
<?php
}
?>
The same method is good for multi-part forms, where you can set action='step1' or action='step2'