[英]PHP form validation on same page with external processing
I am trying to wrap up this contact/quote form which has same page validation but external processing. 我试图包装这种具有相同页面验证但外部处理的联系方式/报价单形式。 I have set up a variable to go in the form action and the variable/url changes from the same page to the processing page when the form validates. 我设置了一个变量以执行表单操作,并且当表单验证时,变量/ URL从同一页面更改为处理页面。 However, it is taking two clicks on the submit button to process the form after all the required fields have been filled in: All the required fields will be filled in, I click submit, the page reloads with the saved data variables and then when I hit submit agin, it finally goes through, sending the email and loading the thankyou page. 但是,填写完所有必填字段后,需要两次单击“提交”按钮来处理表单:将填写所有必填字段,单击“提交”,页面重新加载已保存的数据变量,然后在点击Submit agin,它终于完成了,发送了电子邮件并加载了Thankyou页面。 I have searched the posts here and tried multiple things but have not found a solution. 我在这里搜索了帖子,并尝试了多种方法,但没有找到解决方案。 I am definitely not a php expert, still a newbie so this may not be the best way to accomplish this but I'd appreciate any ideas on how to finish this up. 我绝对不是PHP专家,还是新手,因此这可能不是实现此目标的最佳方法,但我希望对完成此工作提出任何想法。 Here is what I have: 这是我所拥有的:
<?php
....
if (empty($Name) && empty($Company) && empty($Address1) && empty($City) && empty($State) && empty($Phone))
{
echo '<p class="tan">The fields marked with an * are required.</p>';
$Process = 'samepageurl';
}
/*else if (empty($Name) || is_numeric($Name))
{
echo '<p class="tan"><b>Please enter your name.</b></p>';
}*/
else if (empty($Company) || is_numeric($Company))
{
echo '<p class="tan"><b>Please enter your company name.</b></p>';
$Process = 'samepageurl';
}
else if (empty($Address1) || is_numeric($Address1))
{
echo '<p class="tan"><b>Please enter your address.</b></p>';
$Process = 'samepageurl';
}
else if (empty($City) || is_numeric($City))
{
echo '<p class="tan"><b>Please enter your city.</b></p>';
$Process = 'samepageurl';
}
else if (empty($State) || is_numeric($State))
{
echo '<p class="tan"><b>Please enter your state.</b></p>';
$Process = 'samepageurl';
}
else if (empty($Phone) || ctype_alpha($Phone))
{
echo '<p class="tan"><b>Please enter your phone number.</b></p>';
$Process = 'samepageurl';
}
else if (strlen($Phone) < 10 || strlen($Phone) > 12 || ctype_alpha($Phone) || ctype_space($Phone))
{
echo '<p class="tan"><b>Please enter a phone number with an area code.</b></p>';
$Process = 'samepageurl';
}
else if (isset($Name) && isset($Company) && isset($Address1) && isset($City) && isset($State) && isset($Phone))
{
$Process = 'processingurl';
}
?>
<form action="<?php echo $Process; ?>" method="post" class="print" >
<p><input type="hidden" name="recipient" value="responses@url.com"/>
<input type="hidden" name="subject" value="Web Site Response"/>
<input type="hidden" name="redirect" value="thankyou.html"/></p>
... form fields ...
</form>
Thank you in advance! 先感谢您!
First check for missing variables, then extract and validate the variables, then serve content based on them. 首先检查缺少的变量,然后提取并验证变量,然后根据它们提供内容。
<?php
function verifyPostContains(&$req) {
global $_POST;
$missing = array();
foreach($req as $var => $_) {
if(!isset($_POST[$var])) {
$missing[] = $var;
}
}
return $missing;
}
$requirements = array('name'=>'','city'=>'','state'=>'',...);
$missing = verifyPostContains($requirements);
if(count($missing)>0) {
$content = formErrorReport($missing);
sendHeaders();
echo $content;
exit();
}
// extract, making sure to sanitize
$name = sanitize($_POST["name"]);
...
$errorHtml = array();
// validate by reference. Effectively call testName($name).
if(failsValidation($name, "testName")) {
$errorHtml [] = generateError(NAME_ERROR, $name);
} else { $requirements["name"] = $name; }
if(failsValidation($city, "testCity")) {
$errorHtml [] = generateError(CITY_ERROR, $city);
} else { $requirements["city"] = $name; }
...
if(count($errorHTML)>0) {
generateErrorPage($requirements, $missing, $errorHTML);
} else { processForm($requirements); }
?>
this code assumes you have functions to do the various bits that need to be done, and has some string constants for generating error HTML. 此代码假定您具有执行所需的各种操作的功能,并且具有一些用于生成错误HTML的字符串常量。
As a newcomer you may want to google for some tutorials that explain doing form processing using PHP at the server, and JavaScript at the client. 作为新手,您可能想在Google上搜索一些教程,这些教程说明在服务器上使用PHP在客户端上使用JavaScript进行表单处理。 If you find a tutorial that gives you code that echos errors while it's testing the data, such as you code does, move along. 如果您找到了一个教程,该教程为您提供了在测试数据时回显错误的代码(例如您的代码),那么请继续学习。 It's not a good tutorial. 这不是一个很好的教程。 If you find one that stops after it finds one error, move along too. 如果您发现一个在发现错误后停止的故障,也请继续进行。 If you find one that tells you to make sure the values are right in JavaScript, and then says "we already validated this at the client so we use the values directly in PHP", move along, too. 如果找到一个告诉您确保JavaScript值正确的消息,然后说“我们已经在客户端验证了这一点,那么我们可以直接在PHP中使用这些值”,也请继续。 Look for a tutorial that explains: 查找说明以下内容的教程:
(Bonus points if the tutorial explains that a POST request is not required to actually ever generate page content as a response, other than a header that indicates whether or not the POST call was accepted or rejected.) (如果本教程解释为实际上不需要生成POST内容就不需要POST请求,而是指出指示是否接受或拒绝POST调用的标头,则可以得到奖励)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.