我目前正在将课程作为我的网页设计课程的一部分,而我的最终项目需要创建一个验证注册表。虽然表单正确验证了数据,但我无法保存,以便在用户提交数据后保存所输入的任何数据。课程指示我使用'提取物'重新填充表单输入的功能,但我过去也使用过这个问题。以下是我的表单的代码:
<?php
$labels = array("first_name" => "First Name*:",
"last_name" => "Last Name*:",
"company" => "Company*:",
"address" => "Address*:",
"city" => "City*:",
"province_state" => "Province/State*:",
"postal_zip" => "Postal/Zip Code*:",
"phone" => "Telephone:",
"fax" => "Fax:",
"email" => "Email Address*:",
"comments" => "Comments:");
$submit = "Submit";
?>
...
<form method="post">
<table class='form'>
<?php
foreach($labels as $label => $field)
{
if($label == 'postal_zip')
{
echo "<tr><td class='label'><label for='$label'>$field</label></td>
<td class='input'><input type='text' name='$label' maxlength='9'></td></tr>";
}
if($label == 'phone' or $label == 'fax')
{
echo "<tr><td class='label'><label for='$label'>$field</label></td>
<td class='input'><input type='text' name='$label' maxlength='14'></td></tr>";
}
if($label == 'comments')
{
echo "<tr><td class='label'><label for='$label'>$field</label></td>
<td class='input'><textarea name='$label' cols='30' rows='5'></textarea></td></tr>";
}
else
{
if($label != 'postal_zip' && $label != 'phone' && $label != 'fax' && 'comments')
{
echo "<tr><td class='label'><label for='$label'>$field</label></td>
<td class='input'><input type='text' name='$label' value='$value'></td></tr>";
}
}
echo "</tr>";
}
echo "<tr><td></td>
<td><input type='hidden' name='submitted' value='yes'>
<input type='submit' name='$submit' value='$submit'></td>";
?>
</table>
</form>
以下是我的验证码。
<?php
$message = "";
if(isset($_POST['submitted']) and $_POST['submitted'] == 'yes')
{
foreach($_POST as $field => $value)
{
if(empty($value) or !empty($value))
{
$email_patt = "/^.+@.+\\..+$/";
$name_patt = "/^[A-Za-z' -]{1,50}/";
$addr_patt = "/^[A-Za-z0-9 .,'-]{1,50}$/";
$postal_patt = "/^[A-Za-z0-9-]{1,10}$/";
$phone_patt = "/^[0-9)(xX -]{7,20}$/";
if(preg_match("/first_name/i",$field))
{
if(!preg_match($name_patt,$value))
{
$error_array[] = "Please enter a valid first name.";
}
}
if(preg_match("/last_name/i",$field))
{
if(!preg_match($name_patt,$value))
{
$error_array[] = "Please enter a valid last name.";
}
}
if(preg_match("/address/i",$field))
{
if(!preg_match($addr_patt,$value))
{
$error_array[] = "Please enter a valid address.";
}
}
if(preg_match("/company/i",$field))
{
if(!preg_match($addr_patt,$value))
{
$error_array[] = "Please enter a valid company name.";
}
}
if(preg_match("/city/i",$field))
{
if(!preg_match($addr_patt,$value))
{
$error_array[] = "Please enter a valid city.";
}
}
if(preg_match("/country/i",$field))
{
if(!preg_match($addr_patt,$value))
{
$error_array[] = "Please enter a valid address.";
}
}
if(preg_match("/postal_zip/i",$field))
{
if(!preg_match($postal_patt,$value))
{
$error_array[] = "Please enter a valid postal/zip code.";
}
}
if(preg_match("/email/i",$field))
{
if(!preg_match($email_patt,$value))
{
$error_array[] = "Please enter a valid email address.";
}
}
}
$good_data[$field] = strip_tags(trim($value));
}
if(@sizeof($error_array) >0)
{
$message = "<ul class='error'>";
foreach($error_array as $value)
{
$message .= "<li>$value</li>";
}
$message .= "</ul>";
extract($good_data);
include "contact_form.php";
exit();
}
else
{
header('location: success.html');
}
}
else
{
include "contact_form.php";
}
?>
目前,我已将所有$ _POST值放入数组$ good_data中,并在验证代码中使用print_r,我得到用户输入的所有字段和值的列表(尽管它包含所有输入在页面上)。有没有一种简单的方法可以将这些值放在每个字段中?
答案 0 :(得分:0)
感谢Dagon回答我的问题。通过将$ _POST [$ field]添加到每个字段的值,它可以保留数据并正常运行。