Implode array for PHP checkbox in Form

时光怂恿深爱的人放手 提交于 2019-12-02 17:49:03

问题


I have looked at multiple examples of the implode array and cannot work out why I cannot see if multiple check boxes are selected.

Can I pleas get assistance as to what code I need to add and where? Thanks!

CODE IN 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>

回答1:


Try :-

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



回答2:


If you're using <input name="check[]" type="checkbox" />, you'll already get an array in $_POST['check']. You're having an error because you're trying to concatenate an array $check_msg in a string $body.

You need to do the following:

$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;      

If $_POST['check'] isn't an array, you won't get a PHP error.



来源:https://stackoverflow.com/questions/25933138/implode-array-for-php-checkbox-in-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!