在编写我的网站时,我遇到了以下问题:在第一页上,您可以输入一些数据,如姓名,日期,地址等。此外,还有一些带有固定数据的复选框。在填写表格之后,我希望它能够发送到我的电子邮件,这在大多数情况下是有效的。
<?php
//subject of the e-mail
$subject = "Test";
// the message
$msg = "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
"Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
sendMailInt($subject, $msg);
?>
现在我想在我的网站上添加一些带有复选框的输入,这也可以达到某一点。
<?php
//subject of the e-mail
$subject = "Test";
// the message
$msg = "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
"Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>" .
"checkbox1: " . $_POST['checkbox1'] . "<br>" .
"checkbox2: " . $_POST['checkbox2'] . "<br>" .
"checkbox3: " . $_POST['checkbox3'] . "<br>" .
"checkbox4: " . $_POST['checkbox4'] . "<br>" .
"checkbox5: " . $_POST['checkbox5'] . "<br>" .
"checkbox6: " . $_POST['checkbox6'] . "<br>" .
"checkbox7: " . $_POST['checkbox7'] . "<br>" .
"checkbox8: " . $_POST['checkbox8'] . "<br>" .
;
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
sendMailInt($subject, $msg);
?>
特别是对于复选框输入,我希望有类似if语句的东西来检查盒子是否已被选中。如果未选中某个框,则该值为空,这意味着我的电子邮件中有一个空行。由于我有大约20个复选框,如果连续10个盒子未经检查,它可能会在电子邮件中变得有点混乱。
我尝试将$msg =
编写为HTML邮件并在其中添加PHP代码,但随后我的网站停止工作(完全空白,我之前遇到过几次错误,当使用错误的代码时)
<?php
//subject of the e-mail
$subject = "Test";
// the message
$msg = "
<html>
<head>
<title>Test Mail</title>
<body>
<?php
echo "Antragssteller: " . $_POST['ANachname'] . ", " . $_POST['AVorname'] . "<br>E-Mail: " . $_POST['AMail'] . "<br><br>" .
"Testperson: " . $_POST['MNachname'] . ", " . $_POST['MVorname'] . "<br>Adresse: " . $_POST['Adresse'] . "<br><br>";
if ($_POST['checkbox1'] == true)
echo $_POST['checkbox1'] . "<br>";
else
echo "";
if ($_POST['checkbox2'] == true)
echo $_POST['checkbox2'] . "<br>";
else
echo "";
if ($_POST['checkbox3'] == true)
echo $_POST['checkbox3'] . "<br>";
else
echo "";
if ($_POST['checkbox4'] == true)
echo $_POST['checkbox4'] . "<br>";
else
echo "";
if ($_POST['checkbox5'] == true)
echo $_POST['checkbox5'] . "<br>";
else
echo "";
if ($_POST['checkbox6'] == true)
echo $_POST['checkbox6'] . "<br>";
else
echo "";
if ($_POST['checkbox7'] == true)
echo $_POST['checkbox7'] . "<br>";
else
echo "";
if ($_POST['checkbox8'] == true)
echo $_POST['checkbox8'] . "<br>";
else
echo "";
?>
</body>
</html>
";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
sendMailInt($subject, $msg);
?>
有没有办法在邮件程序变量中跳过空变量,以便发送的邮件看起来干净而没有漏洞?
期待收到你们的回复并提前致谢。
您可以做的第一件事是在您正在使用的文件中强制在页面上显示错误(仅在开发环境中使用它):
error_reporting(E_ALL);
ini_set('display_errors', 1);
其次,通过将它们放入数组中,可以大大简化相同数据类型的多个表单输入。这是语法的样子:
<input name="checkbox[]" value="1" />
<input name="checkbox[]" value="2" />
<input name="checkbox[]" value="3" />
<input name="checkbox[]" value="4" />
<input name="checkbox[]" value="5" />
然后在PHP中你可以做这样的事情,只打印出所选的复选框:
foreach ($_POST['checkbox'] as $checkbox) {
echo $checkbox . "<br>";
}