1

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 = '[email protected]';
    $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

2 Answers 2

1

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>;";
Sign up to request clarification or add additional context in comments.

2 Comments

after changing to $check_msg .= "Checked: $value\\n"; i got the below output in mail Products: Checked: Bitter Gourd\nChecked: Ladies Finger\nChecked: Bottle Gourd\n
Probably you will have to use a different variable for mail. Like $check_msg .= "Checked: $value\\n";$check_msg_mail .= "Checked: $value\n";
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.