Re: presenting a failed form for re-entry.
| From: | David Rose | Date: | Sat, 09 Sep 2000 21:43:17 +0000 |
| Subject: | Re: presenting a failed form for re-entry. | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-16021@lists.php.net to get a copy of this message | ||
The way I do it is kinda neat, I think:
A page is presented with both required and optional fields. If a user
misses one or more required fields, that page is presented again, but
only displaying the required fields that were skipped. The values from
each page of the form are transmitted throught subsequent pages as
hidden fields.
index.php:
<form action="<?=$PHP_SELF;?>" method="post">
<table>
<?
$hidden="";
foreach ($HTTP_POST_VARS as $key=>$value)
{$hidden .= "<input type=\"hidden\" name=\"$key\"
value=\"$value\">";}
print $hidden;
if (!exists($page)){$page="1";}
if ($firstname=="" || $lastname=="" || $email=="") { #if any required
fields are blank
$page="1"; $pageSeen="1";
}
else {$page="2"; $pageSeen="1";}
# and more of the same for subsequent form pages
include "page$page.php";
?>
</table>
</form>
page1.php:
<? if ($firstname==""){ ?>
<tr>
<td>First Name: <input type="text" name="firstname"
value="<?=$fistname;?>"></td>
</tr>
<? } ?>
<? if ($pageSeen != "1") { ?>
<tr>
<td>Optional Info <input type="text" name="optinfo"
value="<?=$optinfo;?>"></td>
<? } ?>
Another thing that's cool to use the HTTP_POST VARS for is to generate
SQL INSERT queries for your submit script, and also to build your SQL
CREATE TABLE scripts:
foreach($HTTP_POST_VARS as $key=>$value) {
$fields .= "$key, ";
$values .= "'\$$key', ";
$SQL .= "$key CHAR(48),\n";
}
$query = "INSERT INTO tableName ($key) VALUES ($values)";
print "$query<br><br>$SQL;
#now put that where your submit script would go, and it will spit out
your sql query. You'll need to clean it up a hair (take unused fields
out, and strip that last comma)
#it also spits out the SQL CREATE TABLE script, which you'll add the
CREATE stuff to, and modify field types and lengths.
#This saves a lot of typo-prone grunt work for me.
David Rose
david@sxsw.com