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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?