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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机