Check box selections to email problem

Discussion in 'PHP' started by fozzie, Jan 22, 2007.

  1. #1
    Hi!
    I'm another confused new user.

    Have built a form and everything is fine but I wanted to send the form results to email. The results display correctly as a confirmation on my webpage after I submit the form. The name, email address and message also display in the email confirmation but whenever I try to display the choices from the check boxes, 'array' is printed. The code is below.

    <?php
    echo "<p><font face=\"arial\">Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
    echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";
    echo "<p>Your product choices are:<br/>";
    if (!empty($_POST["brochure"])) {
    echo "<ul>";
    foreach ($_POST["brochure"] as $value) {
    echo "<li>$value</li>";
    }
    echo "</ul>";
    }
    echo "<p>Your message was:<br />";
    echo $_POST["message"]."</p>";
    echo "</font>";
     
    $msg = "<p><strong>Name:</strong> ".$_POST["name"]."</p>";
    $msg .= "<p><strong>E-Mail:</strong> ".$_POST["email"]."</p>";
    $msg .= "<p><strong>Message:</strong> ".$_POST["message"]."</p>";
    $msg .= "<p><strong>Brochures</strong> ".$_POST["brochure"]."</p>";
    //Getting the right mail string on line above is the problem:mad: 
     
     
    $recipient = "seangriffin15@yahoo.co.uk";
    $subject = "Form Submission Results";
    $mailheaders = "MIME-Version: 1.0\r\n";
    $mailheaders .= "Content-type: text/html; charset=ISO-8859-1\r\n";
    $mailheaders .= "From: My Web Site <defaultaddress@yourdomain.com> \n";
    $mailheaders .= "Reply-To: ".$_POST["email"];
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    ?>
    PHP:
    The HTML for the form is here.

    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Straightaway">Straightaway </td>
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Fa&ccedil;ade">Fa&ccedil;ade</td>
    </tr>
     
    <tr valign="top"> 
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Straightaway 100SS"> Straightaway 100SS</td>
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Movable Walls"> Movable Walls</td>
    </tr>
    <tr valign="top"> 
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Glassmaster 100"> Glassmaster 100</td>
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Wallmaster"> Wallmaster </td>
    </tr>
     
    <tr valign="top"> 
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Interior Sliding"> Interior Sliding </td>
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Automatic Operators"> Automatic Operators </td>
    </tr>
     
    <tr valign="top"> 
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Foldaside & Cornaway"> Foldaside & Cornaway</td>
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Supplementary Door Gears"> Supplementary Door Gears </td>
    </tr>
     
    <tr valign="top"> 
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Buyers Guide"> Buyers Guide</td>
    <td width="50%" class="copy"> <input type="checkbox" name="brochure[]" value="Glidemaster"> Glidemaster </td>
    </tr>]
    HTML:
    I've been trying different loops but nothing seems to work. Everything is great apart from that one bit of PHP that i can't get right. Its probably really easy so I thought someone might have a quick solution.

    Thanx for your help guys!!
     
    fozzie, Jan 22, 2007 IP
  2. StanUK

    StanUK Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to build a string based on whats in your array, consider this example:

    <?
    if(isset($_GET['fruits']))
    {
      $FruitList = "";
      foreach($_GET['fruits'] as $Fruit)
        $FruitList .= $Fruit . "<br />";
      echo ("Chosen Fruits were: <br /><b>" . $FruitList . "</b><br />");
    }
    ?>
      
    <form action="<?=$_SERVER['PHP_SELF'];?>" method="get">
    Select: <br />
    <input type="checkbox" value="Apples" name="fruits[]"> Apples <br />
    <input type="checkbox" value="Oranges" name="fruits[]"> Oranges <br />
    <input type="checkbox" value="Bananas" name="fruits[]"> Bananas <br />
    <input type="checkbox" value="Mangos" name="fruits[]"> Mangos <br />
    
    <input type="submit" name="submit" value="Check ->">
    </form>
    PHP:
    This lets the user check different types of fruits, then loops the array upon submit and concatanates a string based on what is in the array.

    Greets.
     
    StanUK, Jan 22, 2007 IP