【发布时间】: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>";
}
?>
【问题讨论】:
-
表格在哪里?表单丢失。