从表单中提取输入的数据(PHP)
我目前正在学习一门课程,作为我的网页设计课程的一部分,我的最终项目需要创建一个验证注册表。虽然表单正确验证了数据,但我无法让它保存用户提交后输入的任何数据。课程指导我使用“提取”函数重新填充表单输入,但我在过去使用该函数时也遇到了很多问题。以下是我的表格代码:从表单中提取输入的数据(PHP),php,forms,validation,Php,Forms,Validation,我目前正在学习一门课程,作为我的网页设计课程的一部分,我的最终项目需要创建一个验证注册表。虽然表单正确验证了数据,但我无法让它保存用户提交后输入的任何数据。课程指导我使用“提取”函数重新填充表单输入,但我在过去使用该函数时也遇到了很多问题。以下是我的表格代码: <?php $labels = array("first_name" => "First Name*:", "last_name" => "Last Name*:",
<?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>
...
感谢达贡对我问题的回答。通过向每个字段的值添加$\u POST[$field],它可以正确地保留数据和功能。感谢Dagon对我的问题的回答。通过向每个字段的值添加$_POST[$field],它将正确保留数据和功能。所有验证都将失败一些有效数据-在执行验证时请仔细考虑。我还应该提到,我尝试将value='“$value.”字段添加到我的输入字段,并在$_POST[$value]下定义它们,但这似乎也不起作用。请始终避免使用extract()
函数。这是一个可怕的概念,可能应该从loopQuestion中的languagevalue=$\u POST[$label]中删除:if(empty($value)或!empty($value))
这永远是真的,那么为什么要有一个IF-there-atall呢?所有的验证都会失败一些有效的数据-在进行验证时请仔细考虑我还应该提到我尝试将value='“$value.”字段添加到我的输入字段中,并在$_POST[$value]下定义了它们,但这似乎也不起作用。始终避免使用extract()
功能。这是一个可怕的概念,可能应该从loopQuestion:if(empty($value)或!empty($value))中的languagevalue=$\u POST[$label]中删除。
这将始终是真的,那么为什么要使用if-there-atall呢。
<?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";
}
?>