Form中的Implode for PHP复选框

时间:2014-09-19 11:41:23

标签: php arrays implode

我已经查看过implode数组的多个示例,但无法确定为什么我看不到是否选中了多个复选框。

我可以请求帮助我需要添加哪些代码以及在哪里添加?谢谢!

PHP代码:     

    $to='myemail@gmail.com';
    $subject='Enquiry - Contact Page';

    $name_field = $_POST['firstname'];
    $secondname_field = $_POST['lastname'];
    $email_field = $_POST['email'];
    $phone_field = $_POST['phone'];
    $dropdown = $_POST['drop_down'];
    $message = $_POST['message'];
    $check_msg = $_POST['check'];

    $body="F-Name: ".$name_field ."\r\n". "L-Name: ".$secondname_field ."\r\n". "Email: ".$email_field ."\r\n". "Phone: ".$phone_field ."\r\n". "Contact-Method: ".$dropdown ."\r\n". "Message: ".$message ."\r\n". $check_msg;      

    mail($to,$subject,$body);
    echo '  ';

    ?>

CODE IN .HTML

            <form method="post" action="contactform.php">
                        <input name="firstname" type="text" value="First Name" class="form2"/><br/>
                        <input name="lastname" type="text" value="Last Name" class="form2"/><br/>
                        <input name="email" type="text" value="Email" class="form2"/>   <br/>
                        <input name="phone" type="text" value="Phone" class="form2"/>   
                        <br />
                        <select name="drop_down" class="form2">
                        <option selected="selected">Prefered contact method
                        </option>
                        <option>Phone</option>
                        <option>Email</option>
                        </select><br />
                        <textarea name="message" cols="30" rows="5" class="form2">About your project..</textarea> 
                        <br />
                        <span class="form">
                        I am looking for...<br /> 
                        <input name="check[]" type="checkbox" value="New-Website" />A new website<br/>
                        <input name="check[]" type="checkbox" value="Existing-Website" />Upgrade an exsiting website<br/>
                        <input name="check[]" type="checkbox" value="Online-Store"/>Online Store<br/>
                        <input name="check[]" type="checkbox" value="Graphic-Design"/>Graphic and logo design<br/>
                        <input name="check[]" type="checkbox" value="Other"/>Other<br/>
                        <br />                  
                        </span>
                        <input name="Submit" type="submit" value="submit" class="form"/>
                    </form>

2 个答案:

答案 0 :(得分:4)

尝试: -

$check_msg = implode(", ", $_POST['check']);

答案 1 :(得分:0)

如果您正在使用<input name="check[]" type="checkbox" />,那么您已经在$_POST['check']中获得了一个数组。您遇到错误,因为您正在尝试在字符串$check_msg中连接数组$body

您需要执行以下操作:

$check = isset($_POST['check']) ? $_POST['check'] : '';
$check_msg = is_array($check) ? implode(", ", $check) : '';
$body = "F-Name: ".$name_field ."\r\n". "L-Name: ".$secondname_field ."\r\n". "Email: ".$email_field ."\r\n". "Phone: ".$phone_field ."\r\n". "Contact-Method: ".$dropdown ."\r\n". "Message: ".$message ."\r\n". $check_msg;      

如果$_POST['check']不是数组,则不会出现PHP错误。