1

Im trying to write a contact form and so far everything works only theres a multiple checkbox in the form and im unsure how to call all data and so my email returns 'array' for the variable 'service'

My code is...

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$service = $_POST['service'];
$description = $_POST['description'];
$location = $_POST['location'];
$to = "[email protected]";

//begin of HTML message
$message = "
From : $name,
Email: $email,
Number: $number,
Service: $service,
Description: $description  
Location: $location
";
//end of message

$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers  .= "From: $email\r\n";

mail($to, $subject, $message, $headers);
?> 

My form for the service input is...

 <dt><input type="checkbox" value="Static guarding" name="service[]" class="cbox">Static guarding</dt>
    <dt><input type="checkbox" value="Mobile Patrols" name="service[]"class="cbox">Mobile Patrols</dt>
    <dt><input type="checkbox" value="Alarm response escorting" name="service[]"class="cbox">Alarm response escorting</dt>
    <dt><input type="checkbox" value="Alarm response/ Keyholding" name="service[]"class="cbox">Alarm response/ Keyholding</dt>
    <dt><input type="checkbox" value="Other" name="service[]"class="cbox">Other</dt>
    <dt><input type="hidden" value="Other" name="service[]"class="cbox"></dt>
6
  • 2
    Do var_dump($_POST['service']); to view the structure of the array within the $_POST array. You have a multi-dimensional array here. Commented Oct 19, 2011 at 20:26
  • that returns... array(3) { [0]=> string(15) "Static guarding" [1]=> string(24) "Alarm response escorting" [2]=> string(5) "Other" } Im unsure how to just call the values though Commented Oct 19, 2011 at 20:29
  • Do you want them all to be listed? Check out my answer. Commented Oct 19, 2011 at 20:30
  • Yeah I need to list them all, Ill google multi dimensional arrays thanks Commented Oct 19, 2011 at 20:31
  • 1
    Your code is vulnerable to e-mail header injection, since you don't sanitize $email. I'd suggest using FILTER_SANITIZE_EMAIL with filter_input() or filter_var() from the Filter extension. Commented Oct 19, 2011 at 20:41

2 Answers 2

4

You can process the checkbox values into a string with implode():

$checkboxes = implode(',', $_POST['service']);

$message = <<<EOL
...
Service: $checkboxes

EOL;

Note the use of a heredoc - it's a MUCH nicer way of defining a multi-line string than using a regular quoted string.

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

1 Comment

Nice answer, just gonna point out you're missing a bracket in the first line and also an argument for implode().
1

Because you appended your form input names with brackets, PHP will form an indexed array out of them.

$sCount = count($service);
for ($s=0; $s<$sCount; $s++) {
    echo $service[$s];
}

You might be better off creating an associative array:

<dt><input type="checkbox" value="Mobile Patrols" name="service[mobile]"class="cbox">Mobile Patrols</dt>
<dt><input type="checkbox" value="Alarm response escorting" name="service[escorting]"class="cbox">Alarm response escorting</dt>

That way, in your script, you could access them as:

echo $service['mobile'];
echo $service['escorting'];

Note that empty checkboxes aren't submitted so you'll want to first check if the array element is set.

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.