Page 1 of 1

checkbox array passing to email - please help

Posted: Wed Dec 03, 2008 10:40 pm
by kitegirl
Hello there,
This is a very simple question!! - But all my hours of searching result in a thousand different answers - none of which is exactly what i need. I have found this forum super yesterday - so hoping for the same today please. !!
Many thanks in advance,
Katie

I have a simple form with checkboxes. This is an html document
I have a php file which processes form and sends to email.

The email I am getting has the checkboxes value as "array". I have found the answers online about looping and imploding etc - just can't find the code!

What is the PHP code i am to use in order for my email to just give me the values that have been checked.?

Here is the html bit:

Code: Select all

 
<input name="product[]" type="checkbox" id="wallart" value="Wall Art" />
      Wall Art<br>
        <input name="product[]" type="checkbox" id="mobile" value="Mobile" />
        Mobile
        <br>
        <input name="product[]" type="checkbox" id="blkboard" value="Blackboard decals" />
        Blk Board Decals
        <br>
        <input name="product[]" type="checkbox" id="screens" value="Screens" />
        Screens
        <br>
        <input name="product[]" type="checkbox" id="letnum" value="Letnum" />
        Acrylic Letters & Numbers <br>
        <input name="product[]" type="checkbox" id="shapes" value="Shapes" />
      Acrylic shapes</p>
Here is all my php code:

Code: Select all

 
<?php
/* Set e-mail recipient */
$myemail  = "blah@blah.com";
 
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$address  = check_input($_POST['address']);
$product   = check_input($_POST['product']);
$screens  = check_input($_POST['screens']);
 
$how_find = check_input($_POST['how']);
$from = check_input($_POST['email']);
$comments = check_input($_POST['comments'], "Write your comments");
 
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}
 
 
 
/* Let's prepare the message for the e-mail */
$message = "Hello!, 
 
Your contact form has been submitted by:
 
Name: $yourname
E-mail: $email
Address: $address
 
Which products I am interested in? 
$product - THIS IS THE BIT I NEED HELP WITH PLEASE????
 
 
 
How did he/she find it? $how_find
 
Comments:
$comments
 
End of message
";
 
/* Send the message using mail() function */
mail($myemail, $subject, $message, "From: $from");
 
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
 
/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}
 
function show_error($myError)
{
?>
    <html>
    <body>
 
    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>
 
    </body>
    </html>
<?php
exit();
}
?>

Re: checkbox array passing to email - please help

Posted: Wed Dec 03, 2008 11:25 pm
by pcoder
Use instead of

Re: checkbox array passing to email - please help

Posted: Wed Dec 03, 2008 11:34 pm
by kitegirl
thanks Pcoder - at least that bits working...

now for the answer I await.....

:lol:

Re: checkbox array passing to email - please help

Posted: Thu Dec 04, 2008 12:06 am
by pcoder
Try this first:

Code: Select all

 
print '<pre>';
print_r($_POST);
print '</pre>';
 
This will definitely helps you to go through the post value.
You will get an array like this:

Code: Select all

 
Array
(
    [product] => Array
        (
            [0] => Wall Art
            [1] => Mobile
            [2] => Blackboard decals
        )
 
    [submit] => Submit
)
 
Then , you have to go through the array to get the value.
Cheers

Re: checkbox array passing to email - please help

Posted: Thu Dec 04, 2008 12:17 am
by kitegirl
Thanks Pcoder - clearly out of my depth - because I don't know where you want me to place the array code you have written?

Do I copy the whole thing and put it....in the php doc somewhere?

Re: checkbox array passing to email - please help

Posted: Thu Dec 04, 2008 12:39 am
by pcoder
Ok, i have edited the message part of your code to get the product value.

Code: Select all

 
 
<?php
/* Set e-mail recipient */
$myemail  = "blah@blah.com";
 
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$address  = check_input($_POST['address']);
$product   = check_input($_POST['product']);
$screens  = check_input($_POST['screens']);
 
$how_find = check_input($_POST['how']);
$from = check_input($_POST['email']);
$comments = check_input($_POST['comments'], "Write your comments");
 
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}
 
 
 
/* Let's prepare the message for the e-mail */
$message = "Hello!,
 
Your contact form has been submitted by:
 
Name: $yourname
E-mail: $email
Address: $address
 
Which products I am interested in?";
$message .= "<ul>";
foreach($_POST['product'] as $key=>$val){
    $message .= "<li>".$val."</li>";
}
$message .= "</ul>";
 
 
 
$message .= " 
How did he/she find it? $how_find
 
Comments:
$comments
 
End of message
";
 
/* Send the message using mail() function */
mail($myemail, $subject, $message, "From: $from");
 
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
 
/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}
 
function show_error($myError)
{
?>
    <html>
    <body>
 
    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>
 
    </body>
    </html>
<?php
exit();
}
?>
 
May be it will give some idea to get the value of such type of an array.
Cheers

Re: checkbox array passing to email - please help

Posted: Thu Dec 04, 2008 12:50 am
by kitegirl
thanks PCoder, - I cut and paste exactly what you put there

and this is the error I received back:
Parse error: syntax error, unexpected '>' in contact.php on line 40

I can't see one = can you? - Line 40 my doc is the line 37 below

If you can't help with this one - thanks anyway. I will keep searching the internet.

Cheers
K