【问题标题】:Ignore empty form fields when sending PHP发送 PHP 时忽略空的表单字段
【发布时间】:2014-09-27 16:06:43
【问题描述】:

我对 PHP 还很陌生,所以我的理解足以让自己感到困惑,但还不足以完成任何富有成效的事情。我有一个注册表单,允许用户添加最多五个学生。每个学生的表单字段都包含在他们自己的 div 类中(.student1、,student2、.student3、.student4 和 .student5)。如果包装的 div 类中的所有字段都留空,我试图阻止 PHP 代码发送数据。

示例 1:如果用户填写了两名学生(.student1 和 .student2)的信息,则只向这两名学生发送电子邮件,并阻止其他三人发送。 示例 2:如果用户只填写了学生的部分信息,仍将所有字段发送给该学生以通过电子邮件发送,而忽略其他完全为空的学生。

我可以实现忽略整个表单中所有空表单字段的代码,但我宁愿只将规则应用于 div 中的所有字段。这有可能吗!?

这是我的代码:

<?php
/* Converts the multiple checkboxs arrays into strings. */
if($_SERVER["REQUEST_METHOD"] == "POST")     {
    foreach($_POST as $key => $val) {
        if(is_array($_POST[$key])) $_POST[$key] = implode('<br>', $_POST[$key]);
    }
}
?>

<?php

if(isset($_POST['submit'])) {

/* POC Info */
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$location = $_POST['location'];
$referral = $_POST['referral'];

/* Student 1 */
$first1 = $_POST['1first'];
$last1 = $_POST['1last'];
$age1 = $_POST['1age'];
$program1 = $_POST['1program'];
$message1 = $_POST['1message'];
$interests1 = $_POST['1interests'];
/* Student 2 */
$first2 = $_POST['2first'];
$last2 = $_POST['2last'];
$age2 = $_POST['2age'];
$program2 = $_POST['2program'];
$message2 = $_POST['2message'];
$interests2 = $_POST['2interests'];
/* Student 3 */
$first3 = $_POST['3first'];
$last3 = $_POST['3last'];
$age3 = $_POST['3age'];
$program3 = $_POST['3program'];
$message3 = $_POST['3message'];
$interests3 = $_POST['3interests'];
/* Student 4 */
$first4 = $_POST['4first'];
$last4 = $_POST['4last'];
$age4 = $_POST['4age'];
$program4 = $_POST['4program'];
$message4 = $_POST['4message'];
$interests4 = $_POST['4interests'];
/* Student 5 */
$first5 = $_POST['5first'];
$last5 = $_POST['5last'];
$age5 = $_POST['5age'];
$program5 = $_POST['5program'];
$message5 = $_POST['5message'];
$interests5 = $_POST['5interests'];

/* Defines the reciever, sender, and email subject */
$to = "email@clientdomain.com";
$sender = $email;
$subject = "New sign-up request from $name";

/* Defines email headers (from, cc, bcc, reply to, etc) and ensures email body is displayed in HTML format */
$headers = "From: $sender\r\n" . "Reply-To: $sender\r\n" . "Content-type: text/html\r\n" . "X-     Mailer: PHP/" . phpversion();

/* Defines the content of the HTML in the email body */
$email_content = <<<EOD
<strong style="color: red;">Point of Contact</strong><br>
Name: $name <br>
Phone: $phone <br>
Email: $email <br>
Location: $location <br>
Referral: $referral <br><br>
<strong style="color: red;">First Student Information</strong><br>
Name: $first1 $last1 <br>
Age Group: $age1 <br>
Program: $program1 <br>
<strong>Interests and Goals</strong><br>
$interests1 <br>
<strong>Additional Information</strong><br>
$message1 <br><br>
<strong style="color: red;">Second Student Information</strong><br>
Name: $first2 $last2 <br>
Age Group: $age2 <br>
Program: $program2 <br>
<strong>Interests and Goals</strong><br>
$interests2 <br>
<strong>Additional Information</strong><br>
$message2 <br><br>
<strong style="color: red;">Third Student Information</strong><br>
Name: $first3 $last3 <br>
Age Group: $age3 <br>
Program: $program3 <br>
<strong>Interests and Goals</strong><br>
$interests3 <br>
<strong>Additional Information</strong><br>
$message3 <br><br>
<strong style="color: red;">Fourth Student Information</strong><br>
Name: $first4 $last4 <br>
Age Group: $age4 <br>
Program: $program4 <br>
<strong>Interests and Goals</strong><br>
$interests4 <br>
<strong>Additional Information</strong><br>
$message4 <br><br>
<strong style="color: red;">Fifth Student Information</strong><br>
Name: $first5 $last5 <br>
Age Group: $age5 <br>
Program: $program5 <br>
<strong>Interests and Goals</strong><br>
$interests5 <br>
<strong>Additional Information</strong><br>
$message5 <br><br>
EOD;


/* Successful pop up message */
echo 
"<script>alert('Congratulations on taking your first step! A member of our team will get in touch     with you as soon as possible.');</script>
<script>window.location = 'http://www.clientdomain.com/success.html'</script>";

/* mail syntax is: reciever, subject, email content, and headers which are all defined above) */
mail($to, $subject, $email_content, $headers);


} else {

/* Failed pop up message */
echo
"<script>alert('Sorry, there seems to be an error with your submission.');</script>
<script>window.location = 'http://www.clientdomain.com/fail.html</script>";
}

?>

【问题讨论】:

  • 表格在哪里?表单丢失。

标签: php forms field


【解决方案1】:

您实际上可以简化很多您拥有的东西。请记住,保持干燥(不要重复自己)。
此外,我正在检查空值,因此您不会收到未定义索引的错误,并且如果不存在则提供默认值。
最重要的是,修剪所有内容,以免在任何内容之前/之后留下一堆空格

不是创建 30 个变量(每个学生 6 个),而是创建 1 个数组,然后在以后循环遍历该数组。如果您想在表单中添加更多字段或学生,有助于在未来保持其可扩展性。

只有在所有字段都为空时,才会将学生信息添加到表单中。如果 1 只使用 1,则给其他的一个默认值,如果它们是空的。

<?php
if (isset($_POST['submit'])) {

    /* POC Info */

    $name=isset($_POST['name'])?trim($_POST['name']):null;
    $name=empty($name)?'Default POC Name':$name;
    $phone=isset($_POST['phone'])?trim($_POST['phone']):null;
    $phone=empty($phone)?'Default POC Phone':$phone;
    $email=isset($_POST['email'])?trim($_POST['email']):null;
    $email=empty($email)?'Default POC Email':$email;
    $location=isset($_POST['location'])?trim($_POST['location']):null;
    $location=empty($location)?'Default POC Location':$location;
    $referral=isset($_POST['referral'])?trim($_POST['referral']):null;
    $referral=empty($referral)?'Default POC referral':$referral;

    $students=array();
    for ($i=0; $i<=5; $i++) {
        $first=trim($_POST[$i.'first']);
        $last=trim($_POST[$i.'last']);
        $age=intval($_POST[$i.'age']);
        $program=trim($_POST[$i.'program']);
        $interests=trim($_POST[$i.'interests']);
        $message=trim($_POST[$i.'message']);

        if (empty($first)&&empty($last)&&empty($age)&&empty($program)&&empty($message)&&empty($interests)) continue;
        $students[]=array(
            'first'=>empty($first)?'unknown':$first,
            'last'=>empty($last)?'unknown':$last,
            'age'=>empty($age)?'unknown':$age,
            'program'=>empty($program)?'unknown':$program,
            'message'=>empty($message)?'unknown':$message,
            'interests'=>empty($interests)?'unknown':$interests,
        );
    }

    /* Defines the reciever, sender, and email subject */
    $to="email@clientdomain.com";
    $sender=$email;
    $subject="New sign-up request from $name";

    /* Defines email headers (from, cc, bcc, reply to, etc) and ensures email body is displayed in     HTML format */
    $headers="From: $sender\r\n"."Reply-To: $sender\r\n"."Content-type: text/html\r\n"."X-     Mailer: PHP/".phpversion();

    /* Defines the content of the HTML in the email body */
    $email_content=<<<EOD
<strong style="color: red;">Point of Contact</strong><br>
Name: $name <br>
Phone: $phone <br>
Email: $email <br>
Location: $location <br>
Referral: $referral <br><br>
EOD;
$student_num=0;
foreach ($students as $student) {
    $email_content.='
<strong style="color: red;">Student '.++$student_num.' Information</strong><br>
Name: '.$student['first'].$student['last'].'<br>
Age Group: '.$student['age'].'<br>
Program: '.$student['program'].'<br>
<strong>Interests and Goals</strong><br>
'.$student['interests'].' <br>
<strong>Additional Information</strong><br>
'.$student['message'].' <br><br>
';
}
?>

【讨论】:

  • 哇,根据您的 cmets,这似乎正是我正在寻找的东西,除了我无法让它工作而且我很确定我知道为什么。在我的 HTML 代码中,我有 5 个单独的学生字段(当然都采用相同的形式),每个字段都有自己的 ID。很明显,你拿走了我的唯一 ID,这是否意味着我也应该在我的 HTML 表单中删除它们,并且只有一个学生块?非常感谢您的帮助!
  • 没有没有。 $_POST[$i.'first'] 就是这样做的。 $i 是一个递增值,因此脚本正在有效地检查 $_POST['1first']$_POST['2first'] 等。是什么没用?是否填充了 $students 数组?在您的脚本中,您可以在for 循环之后放置一个var_dump($students),并确定数据是否存在。
  • 好的,过去两天我一直在努力理解我做错了什么,但我不断收到“致命错误:无法在 /... 中的写入上下文中使用函数返回值” /code/trial-form.php 在第 6 行”提交表单后。我完全复制了您的代码并将其粘贴在代码中的“/*成功弹出消息 */”注释上方。
  • 啊……我的错。我使用 PHP 5.5。我忘了不是每个人都在使用那么高的版本。这很可能是因为empty(trim($_POST['name']))... 检查。在 5.5 之前,empty() 内只能使用变量。我会修复我的代码。
  • @James:我拆分了这些支票。现在应该可以了。 =)
【解决方案2】:

正如您已经描述了一种“模型”,您现在必须在较低级别实现它。

首先将字段分为必填字段和附加字段组。 既然要发送电子邮件,至少邮件字段是必填字段。剩下的就看你自己了。

之后你必须在 php 中实现一个逻辑来检查你的必填字段是否不为空。如果是这样,您可以发送包含您获得的信息的邮件(也许这需要另一个空白检查)。 否则,您拒绝请求并打印您的消息。

您可以在客户端和服务器端执行此操作。在客户端,您必须将 HTML5 属性 required 添加到输入字段。但这不是您可以依赖的东西。它只是保存了一些请求。在服务器端,您必须在发送邮件之前检查所有必填字段。

当您提供更多信息时,我们可以进一步提示您如何在 php 中实现逻辑。

【讨论】:

    【解决方案3】:

    例如,您可以检查姓氏是否已填写并包含 html 代码,如下所示(= 之前的点很重要,它附加到字符串中):

    $email_content = <<<EOD
        <strong style="color: red;">Point of Contact</strong><br>
        Name: $name <br>
        Phone: $phone <br>
        Email: $email <br>
        <br>
        Location: $location <br>
        Referral: $referral <br><br>
        <strong style="color: red;">First Student Information</strong><br>
    EOD;
    
    if(!empty($last1)) {
        $email_content .= <<<EOD
        Name: $first1 $last1 <br>
        Age Group: $age1 <br>
        Program: $program1 <br>
        <strong>Interests and Goals</strong><br>...
    EOD;
    
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2014-09-27
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多