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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵