Question

Actually am having a contact form consist of check-boxes. i am able to send all the form values to email but i don't know how to send values of check-boxes selected in 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> 

and the php script for mail is given below

<?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>";

}

}

?>

can you correct the code and spot where i made mistake. thanks for your time in advance

Was it helpful?

Solution

Change this

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

To

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

Because otherwise in javascript it will throw error, saying string literal is not closed.

Also make the alert like following

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

OTHER TIPS

It is important to know the values of a checkbox are only sent when checked. If you need them sent when they are unchecked create a hidden input with the same name right before the checkbox with the value you want sent when not checked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top