发送PHP时忽略空表单字段

时间:2014-09-27 16:06:44

标签: php forms field ignore

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

示例1:如果用户填写两名学生(.student1和.stu​​dent2)的信息,则仅向这两名学生发送电子邮件,并阻止其他三名学生发送。 示例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>";
}

?>

3 个答案:

答案 0 :(得分:1)

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

创建1个数组,然后在稍后循环遍历数组,而不是创建30个变量(每个学生6个)。如果您想在表单中添加更多字段或学生,请确保将来可扩展。

学生的信息未添加到表单的唯一时间是所有字段为空时。如果仅使用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>
';
}
?>

答案 1 :(得分:0)

正如您已经描述过的一种&#34;模型&#34;你现在必须在较低的层次上实现它。

首先将单独的字段分为强制字段和附加字段组。 由于您要发送电子邮件,因此至少邮件字段是强制性的。其余的由你决定。

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

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

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

答案 2 :(得分:0)

你可以检查例如是否填写了姓氏并且是否包含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;

}
...