0

I am brand new to php. I would like to post multiple checkbox values in an email from a form when they are selected. Only the last option checked is coming through in the email, even if they are all selected in the form. I have tried many solutions I found on for this and can't seem to find one that works for me.

What am I doing wrong?

HTML:

        <input type="checkbox" name="product-types-owned[]" value="Cast_Iron" /> 
        <input type="checkbox" name="product-types-owned[]" value="Braisers" />
        <input type="checkbox" name="product-types-owned[]" value="Ovens" />
        <input type="checkbox" name="product-types-owned[]" value="Skillet" />

php:

        $sendto   = "[email protected]";
        $usermail = $_POST['Email'];
        $firstname  = nl2br($_POST['First_name']);
        $lastname  = nl2br($_POST['Last_name']);
        $address1  = nl2br($_POST['Address1']);
        $address2  = nl2br($_POST['Address2']);
        $city = nl2br($_POST['City']);
        $state = nl2br($_POST['State']);
        $zip = nl2br($_POST['Zip_Code']);
        $phone = nl2br($_POST['Telephone']);
        $ownership = nl2br($_POST['product-types-owned']); 

        $subject  = "Product Registration";
        $headers  = "From: " . strip_tags($usermail) . "\r\n";
        $headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html;charset=utf-8 \r\n";

        $msg  = "<html><body style='font-family:Arial,sans-serif;'>";
        $msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>Product Registration</h2>\r\n";
        $msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
        $msg .= "<p><strong>First Name:</strong> ".$firstname."</p>\r\n";
        $msg .= "<p><strong>Last Name:</strong> ".$lastname."</p>\r\n";
        $msg .= "<p><strong>Address:</strong> ".$address1."</p>\r\n";
        $msg .= "<p><strong>Address Line 2:</strong> ".$address2."</p>\r\n";
        $msg .= "<p><strong>State:</strong> ".$state."</p>\r\n";
        $msg .= "<p><strong>Zip:</strong> ".$zip."</p>\r\n";
        $msg .= "<p><strong>Telephone:</strong> ".$phone."</p>\r\n";
        $msg .= "<p><strong>Products Own or Intend to Own:</strong> ".$ownership."</p>\r\n";
        $msg .= "</body></html>";

I have tried the solution here as well as changing

$ownership = nl2br($_POST['product-types-owned']);

to

$ownership = nl2br(implode(',', $_POST['product-types-owned']));

and I get an error: Warning: implode() [function.implode]: Invalid arguments passed in /home/content/99/11039499/html/scripts/warranty.php on line 24

Please help, I am so frustrated.

3
  • Did you try var_dump($_POST['product-types-owned']) before assigning to $ownership and var_dump($ownership)? Commented Jun 6, 2013 at 16:51
  • Or possibly implode(",", $ownership). Commented Jun 6, 2013 at 16:54
  • @u_mulder i did not, not sure how to go about doing that Commented Jun 6, 2013 at 17:04

1 Answer 1

3
    $ownership = nl2br($_POST['product-types-owned']); 

product-types-owned is going to be an array of the checkbox values that were selected. You'll need to implode that before you do anything else:

$ownership = nl2br(implode(',', $_POST['product-types-owned']));

Right now you're trying to nl2br on an array, which won't work. nl2br expects a string, so php will type cast the array into its default string representation, which is literally the word Array.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. I tried replacing $ownership = nl2br($_POST['product-types-owned']); with $ownership = nl2br(implode(',', $_POST['product-types-owned'])); and I get an error now: Warning: implode() [function.implode]: Invalid arguments passed in /home/content/99/11039499/html/scripts/warranty.php on line 24. I also tried changing nl2br to array, and then i get the same error. can you help?
i was having an issue with my server and the code was not updating properly. $ownership = nl2br(implode(',', $_POST['product-types-owned'])); worked.

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.