如何使用PHP以联系人形式发送复选框值?

时间:2014-03-11 07:56:38

标签: php forms contact-form

实际上我的联系表格包含复选框。我可以将所有表单值发送到电子邮件,但我不知道如何发送在表单中选中的复选框值。

<form id="contact_form" name="contact_form" method="post" action="contact.php">


                <p id="cn">* Post Your Comments</p>
                <table width="0" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><strong>Name:</strong></td>
                    <td><input name="contact_name" type="text" class="inp_txt" /></td>
                  </tr>
                  <tr>
                    <td><strong>Phone:</strong></td>
                    <td><input name="contact_phone" type="text" class="inp_txt" /></td>
                  </tr>
                  <tr>
                    <td><strong>Email:</strong></td>
                    <td><input name="contact_email" type="text" class="inp_txt" /></td>
                  </tr>
                  <tr>
                    <td><strong>Products</strong>:</td>
                    <td>&nbsp;</td>
                  </tr>
                </table>
                <table width="0" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td>Tomato:</td>
                    <td><input type="checkbox" name="check[]" value="Tomato" /></td>
                    <td style="padding-left:20px;">Bitter Gourd:</td>
                    <td style="padding-left:20px;"><input type="checkbox" name="check[]" value="Bitter Gourd" /></td>
                    <td style="padding-left:20px;">Capsicum:</td>
                    <td><input type="checkbox" name="check[]" value="Capsicum" /></td>
                  </tr>
                  <tr>
                    <td>Chillies:</td>
                    <td><input type="checkbox" name="check[]" value="Chillies" /></td>
                    <td style="padding-left:20px;">Ladies Finger:</td>
                    <td style="padding-left:20px;"><input type="checkbox" name="check[]" value="Ladies Finger" /></td>
                    <td style="padding-left:20px;">Cabbage:</td>
                    <td><input type="checkbox" name="check[]" value="Cabbage"/></td>
                  </tr>
                  <tr>
                    <td>Bottle Gourd:</td>
                    <td><input type="checkbox" name="check[]" value="Bottle Gourd" /></td>
                    <td style="padding-left:20px;">Drumstick:</td>
                    <td style="padding-left:20px;"><input type="checkbox" name="check[]" value="Drumstick"/></td>
                    <td style="padding-left:20px;">Beans:</td>
                    <td><input type="checkbox" name="check[]" value="Beans"/></td>
                  </tr>
                  </table>
                <table width="0" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><strong>Location:</strong></td>
                    <td><input name="contact_loc" type="text" class="inp_txt" /></td>
                  </tr>
                  <tr>
                    <td><strong>Comments:</strong></td>
                    <td><textarea name="contact_text" class="txt_txt inp_txt"></textarea></td>
                  </tr>
                </table>
                <table width="0" border="0" cellspacing="0" cellpadding="0" style="margin:0px 0px 0px 77px;">
                  <tr>
                    <td><input type="submit" id="button2" name="submit" value="Submit" onclick="return validation();" class="inp_btn" /></td>
                    <td><input type="submit" value="Reset" class="inp_btn"/></td>
                  </tr>
                </table>
                </form> 

和邮件的PHP脚本在下面给出

<?php
if (isset($_POST['contact_name']) && isset($_POST['contact_phone']) && isset($_POST['contact_email']) && isset($_POST['contact_text'])){

$contact_name= $_POST['contact_name'];
$contact_phone= $_POST['contact_phone'];
$contact_email= $_POST['contact_email'];
$contact_loc= $_POST['contact_loc'];
$contact_text= $_POST['contact_text'];
foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}
echo"<script> alert($check_msg); </script>;";

if (!empty($contact_name) && !empty($contact_phone) && !empty($contact_email) && !empty($contact_text)){
    $to = 'rameshjoe381@gmail.com';
    $subject = 'Enquiry Form Submitted.';
    $body = "Name: ".$contact_name."\n\n"."Phone No: ".$contact_phone."\n\n"."Message: ".$contact_text."\n\n"."Products: ".$check_msg."\n\n"."Location: ".$contact_loc;
    $headers = 'From: '.$contact_email;
    if (@mail($to, $subject, $body, $headers))
    {?>
        <script type="text/javascript">
    alert("Thanks for contacting us.. We\'ll be in touch soon.'");



  </script><?
  echo "<script>window.location.href='index.html' </script>";

    }
    else{
    alert("Error Sending Mail.");
                                        echo "<script>window.location.href='index.html' </script>";

    }
}else{
    alert("All Fields are required.");
                                        echo "<script>window.location.href='index.html' </script>";

}

}

?>

你可以纠正代码并找出我犯错误的地方。 感谢您提前的时间

2 个答案:

答案 0 :(得分:1)

更改此

$check_msg .= "Checked: $value\n";

$check_msg .= "Checked: $value\\n";

因为否则在javascript中它会抛出错误,说字符串文字没有关闭。

同时发出如下警告

echo "<script> alert('$check_msg'); </script>;";

答案 1 :(得分:0)

重要的是要知道复选框的值仅在选中时发送。如果您需要在未选中时发送它们,请在复选框之前创建一个具有相同名称的隐藏输入,并在未选中时发送您要发送的值。