doumu1951 2019-03-04 13:42
浏览 70
已采纳

使用php和if-statement将某些html变量添加到邮件程序变量中

While writing my website I encountered the following Problem: On the first page, you can enter some data like names, dates, addresses etc. In addition, there are a few checkboxes with fixed data. After filling out the form I want it to get sent to my e-mail, which is working for the most part.

<?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);
?>

Now I want to add some input with checkboxes on my website, which also works up to a certain point.

<?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);
?>

Specifically for the checkbox input, I want to have something like an if-statement to check if the box has been checked or not. If a box is unchecked, the value is empty, which means I have an empty line in my e-mail. Since I have around 20 checkboxes, it can get a bit messy in the e-mail, if 10 boxes in a row are unchecked.

I tried writing the $msg = as HTML mail and adding PHP code into it, but then my website stopped working (went completely blank, an error I encountered a few times before, when using wrong code)

<?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);
?>

Is there any way to skip empty variables in the mailer variable, so that the mail that gets sent looks clean without holes?

Looking forward to hearing from you guys and thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanmei1930 2019-03-04 14:15
    关注

    First thing you can do is in the file you're working with to force errors to show on the page (only use this in development environments):

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    Second, multiple form inputs of the same data type can be greatly simplified by putting them in an array. Here's what the syntax would look like:

    <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" />
    

    Then in PHP you could do something like this that will only print out the checkboxes that were selected:

    foreach ($_POST['checkbox'] as $checkbox) {
      echo $checkbox . "<br>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分