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 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。