dsgdhtr_43654 2015-09-22 16:14 采纳率: 100%
浏览 140
已采纳

试图以php格式发送电子邮件复选框[重复]

This question already has an answer here:

I have several checkboxes in a form. And I'm trying to echo those values into an email. Here is the HTML:

<input id="checkbox1" type="checkbox" name="work-check[]"><label for="checkbox2">App Design</label>
                        <input id="checkbox2" type="checkbox" name="work-check[]"><label for="checkbox1">Design</label>
                        <input id="checkbox3" type="checkbox" name="work-check[]"><label for="checkbox2">Email Design</label>
                        <input id="checkbox4" type="checkbox" name="work-check[]"><label for="checkbox1">Layout Design</label>
                        <input id="checkbox5" type="checkbox" name="work-check[]"><label for="checkbox1">Poster Design</label>
                        <input id="checkbox6" type="checkbox" name="work-check[]"><label for="checkbox2">Web Design</label>

And here is the PHP:

    <?php

    if ($_POST["submit"]) {


        if(!filter_var($_POST[email], FILTER_VALIDATE_EMAIL)) {

            echo '<div data-alert class="alert-box warning">E-mail is not valid</div>';
            $error=true;
        }

        if($error==false) {

            $result='<div data-alert class="alert-box success">Form Submitted</div>';

            $headers = array("From: myemail@mail.com",
            "Reply-To: myemail@mail.com",
            "X-Mailer: PHP/" . PHP_VERSION
            );
            $headers = implode("
", $headers);

            // the message
            // the message
        $msg = "
        First Name: ".$_POST['first_name']." 

        Last Name: ".$_POST['last_name']."

        Email: ".$_POST['email']."

        Options: ".implode(",", $_POST['work-check']);


            // use wordwrap() if lines are longer than 70 characters
            $msg = wordwrap($msg,70);

            $to = "myemail@mail.com";

            $subject = "New Request";

            mail($to, $subject, $msg, $headers);
        }
    }

    echo $result;
?>

I think I have it set up that I should get an array back called work-check. I just don't know php well enough to know how to cycle through that array and set the values I get back. I found something like this which I think might be going in the right direction:

        // The array will only contain the checked value of forms
        foreach($golfer as $g) {
       // can use checkbox value here

}

I'm not sure where in the php code that should go.

</div>
  • 写回答

2条回答 默认 最新

  • dqotv26286 2015-09-22 16:34
    关注

    Store it as a string

        //Store the array from the post as an array
        $arr = $_POST['work-check'];
        //implode the array into a string variable
        $options = implode( ",", $arr );
    

    Or, keep it as an array and iterate over it doing what you want.

        $arr = $_POST['work-check'];
        foreach ($arr as &$value) {
            ...
        }
    
        unset($value); // break the reference with the last element
    

    See the PHP manual for some more usage examples of foreach and implode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?