views:

21

answers:

1

Have a form that is emailed with checkboxes.

Some checkboxes have an optional field to enter text. Like "other and more info"

How can I attach the text people entered to those check boxes that this option.

This is one group.

<form action="sendemail.php" method="POST" name="Contact Form">
  <div><input type="checkbox" class="checkbox" name="check[]" value="Word of Mouth?">Word of Mouth?</div>
  <div><input type="checkbox" class="checkbox" name="check[]" value="Have you seen our Sign?  Where?">Have you seen our Sign? Where?<input type="text" style="float:right; width:150px;" name="sign" /></div>
  <div><input type="checkbox" class="checkbox" name="check[]" value="Other.">Other. <input type="text" style="float:right; width:300px;" name="other" /></div>
<p><input type="submit" value="Submit" name="submit"></p>

And my sendmail.php is like:

<?php
if(isset($_POST['submit'])) {

    $to = "[email protected]"; 
    $subject = "Contact Form";
    $firstname_field = $_POST['firstname'];
    $lastname_field = $_POST['lastname'];

    foreach($_POST['check'] as $value) {
        $check_msg_0 .= "  $value\n";
    }

    $body = "From: $firstname_field $lastname_field\n Learned about us from: \n $check_msg_0\n \n ";

    echo "Data has been submitted to $to!";
    mail($to, $subject, $body);

} else {
    echo "Nope didn't work!";
}
?>

UPDATED - Have tried recommendation but did not work. I can't get the value and text field together.

Tried this but still no results.

<form action="sendemail.php" method="POST" name="Contact Form">
  <div><input type="checkbox" class="checkbox" name="check[]" value="Word of Mouth?">Word of Mouth?</div>
  <div><input type="checkbox" class="checkbox" name="check[]" value="Other.">Other. <input type="text" style="float:right; width:300px;" name="otherwhere" /></div>
<p><input type="submit" value="Submit" name="submit"></p>

if (count($_POST['check']) > 0) {
    foreach ($_POST['check'] as $value) {
        if (isset($_POST['check']) && $_POST['check'] == 'Other') {
            $check_msg_0 .= $value.": ".$_POST['otherwhere']."<br>";
        }
        else { 
            $check_msg_0 .= $value."<br>";
        }
     }
}

UPDATE 2 - Got it to work. Changed my textfield name to "otherwhereTXT" and by doing the following:

if (count($_POST['check']) > 0) {
     foreach ($_POST['check'] as $value) {
         if ($value == 'other') {
        $check_msg_0 .= "Other: ".$_POST['otherwhereTXT']."<br>";
         } else { 
        $check_msg_0 .= $value."<br>";
      }
    }
}
A: 

You would use...

$body = "From: $firstname_field $lastname_field\n Learned about us from: \n";

if (count($_POST['check'] > 0 {
    foreach ($_POST['check'] as $value) {
        $body .= $value."\n";
    }
}
if (strlen($_POST['other']) > 0) {
    $body .= "\n\nOther: ".$_POST['other'];
}

This loops through each of your checked values (checkboxes only show up in $_POST if they've been checked) and adds them to the body message.

Webnet
but how would i attach the input like in "Other" when it send the email?
pcasa
I've updated it
Webnet
This works to separate the two but need it as one piece. I updated my question with your recommendation.
pcasa