【问题标题】:PHP POST form validating error [closed]PHP POST表单验证错误[关闭]
【发布时间】:2013-06-26 21:19:34
【问题描述】:

我有一个在联系表单页面上执行的 PHP 表单。我让它运行良好 - 但它目前只检查以确保输入了姓名和电子邮件。我也试图让它检查一条消息,但我的尝试只是因为页面在 php.ini 之后不加载。这是我所拥有的:

<?php 
 $to = "me@gmail.com" ; 
 $from = $_REQUEST['Email'] ; 
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Web Contact Data";
 $startmonth = $_REQUEST['StartMonth'];
 $startyear = $_REQUEST['StartYear'];
 $endmonth = $_REQUEST['EndMonth'];
 $endyear = $_REQUEST['EndYear'];
 $message = $_REQUEST['Message'];

 $fields = array(); 
 $fields{"Name"} = "Name"; 
 $fields{"Email"} = "Email"; 
 $fields{"Phone"} = "Phone"; 

 $selectedProjects  = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
    $selectedProjects = implode(', ', $_POST['projects']);
}
 $selectedSkills  = 'None';
if(isset($_POST['skills']) && is_array($_POST['skills']) && count($_POST['skills']) > 0){
    $selectedSkills = implode(', ', $_POST['skills']);
}
$selectedNoRush  = 'None';
if(isset($_POST['norush']) && is_array($_POST['norush']) && count($_POST['norush']) > 0){
    $NoRush= implode(', ', $_POST['norush']);
}
$selectedWhenReady  = 'None';
if(isset($_POST['whenready']) && is_array($_POST['whenready']) && count($_POST['whenready']) > 0){
    $WhenReady= implode(', ', $_POST['whenready']);
}
$selectedBudget  = 'None';
if(isset($_POST['budget']) && is_array($_POST['budget']) && count($_POST['budget']) > 0){
    $selectedBudget= implode(', ', $_POST['budget']);
} 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}
$body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
$body .= 'Selected Skills: ' . $selectedSkills . "\n\n";
$body .= 'Start Date: ' . $startmonth . " " . $startyear . " " . $NoRush . "\n";
$body .= 'End Date: ' . $endmonth . " " . $endyear . " " . $WhenReady . "\n";
$body .= 'Budget: ' . $selectedBudget . "\n\n";
$body .= 'Message:' . $message . "\n";



 $headers2 = "From: me@gmail.com"; 
 $subject2 = "Thank you for contacting us"; 
 $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";

 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a name, please go back and try again";} 
 else {
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {print "Thank you. Your request has been successfully submitted.";} 
 else 
 {print "We encountered an error sending your mail, please check your details are correct or email us at hello@lyonempire.co.uk"; } 
 }
}
 ?>

所以以上都可以正常工作,但是当我在名称/电子邮件检查后添加以下代码时,它会中断:

if($message == '') {print "You have not entered a message, please go back and try again";} 
 else {

我做错了什么?

谢谢! MC

【问题讨论】:

  • 这是我最近看到的最不寻常的包围样式...
  • it breaks。它以什么方式破裂?你得到什么信息?您的代码在包含的行中的外观如何(仅显示新行和前后两行,而不是仅显示新行)
  • @rr 这是我第一次使用 PHP 代码 - 请让我知道为什么它不寻常,因为我正在将它拼凑在一起,这对了解真的很有帮助...
  • @Arjan 页面的其余部分停止加载,表单和页脚未加载。这是一个缺少的大括号,因为我是个白痴。

标签: php html forms validation


【解决方案1】:
if($name == '')
{
    print "You have not entered a name, please go back and try again";
}
else if($message == '') {
    // do what ever you want
} 
else {
    $send = mail($to, $subject, $body, $headers); 
    $send2 = mail($from, $subject2, $autoreply, $headers2); 

....这里休息

【讨论】:

    【解决方案2】:

    我无法从您显示的代码中看出,但您是否关闭了 else 语句中的大括号?

    if($message == '') {print "You have not entered a message, please go back and try again";} 
    else {} <----
    

    【讨论】:

    • 就是这样,我觉得自己很蠢。
    猜你喜欢
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    相关资源
    最近更新 更多