*PHP help* contact form check box error

scy

Well-Known Member
Joined
Aug 1, 2011
Messages
101
Reaction score
16
Hi all, I need help. I've designed a contact form with html and php but I get "array" instead of the selected check box option when I receive an email from the site.

Html code:
<!DOCTYPE html>
<html>
<head>

<form action="request_quote.php" method="post">
Your name<br>
<input name="name" type="text" size="40"><br>
Contact number<br>
<input name="contact_number" type="text" size="40"><br>
Email<br>
<input name="email" type="text" size="40"><br><br>
Please select desired insurance below:<br>
<input name="insurance_type[]" type="checkbox" value="Commercial lines" /> Commercial lines<br>
<input name="insurance_type[]" type="checkbox" value="Personal lines" /> Personal lines<br>
<input name="insurance_type[]" type="checkbox" value="Life insurance" /> Life insurance<br>
<input name="insurance_type[]" type="Checkbox" value="Specialised services"/> Specialised services<br>
<br>Additional details<br>
<textarea name="message" cols="40"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
</body>
</html>

PHP code:
<?php
$field_name = $_POST['name'];
$field_contact_number = $_POST['contact_number'];
$field_insurance_type = $_POST['insurance_type'];

$field_email = $_POST['email'];
$field_message = $_POST['message'];


$mail_to = '[email protected]';
$subject = 'Request for quotation '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'Contact number: '.$field_contact_number."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Insurance type: '.$field_insurance_type."\n";

$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to [email protected]');
window.location = 'contact-us.html.html';
</script>
<?php
}
?>

Please assist!
 
As well you should - what were you expecting to get?

insurance_type[] is an array being feed to your PHP. It can have no values (ie, not exist) or it can be any and all of your checkbox options. so if you checked all four, insurance_type would be:

array ("Commercial lines" , "Personal lines" , "Life insurance" , "Specialised services")

Hope that helps - otherwise explain what you wanted and we can take it from there.
 
I want to receive the checked option. How do I go about it?
 
You have several options with the same name. So each one that is checked will be parsed. If you only want one to be checked, try using RADIO instead of CHECKBOX:

<INPUT TYPE="radio" NAME="insurance_type" VALUE="Commercial lines">Commercial Lines
<INPUT TYPE="radio" NAME="insurance_type" VALUE="Personal lines">Personal lines

..etc

Using [] sends an array on submission. And that array could have all, none or some of the values. Actually, it won't have none - if nothing is checked, the array will not exist and your form will stall here:

$field_insurance_type = $_POST['insurance_type'];

Another approach would be:
Code:
...
$field_insurance_type = implode(" and " , $_POST['insurance_type']);
...
$body_message .= 'Insurance type(s): '.$field_insurance_type."\n";

This method will show EACH of the checked items.
 
Thank you very much GreGorGy. It works.
 
One thing though, I receive insurance type(s) when multiple check boxes are selected. I want to receive specific information.
 
One thing though, I receive insurance type(s) when multiple check boxes are selected. I want to receive specific information.

Then use the radio buttons. If you have used checkboxes, then multiple selections by the user will be possible.
 
Isn't there a way of allowing multiple selections by the user and receiving selected check boxes?
 
Checkboxes - your first method - allows multiple selections. You have two approaches to getting that data into your php form. First, name each checkbox differently. Second, use an array. Please explain exactly - with example scenarios - you need to achieve with this form, and then I can advise.
 
Suppose a web visitor selects Commercial and Life insurance, I want to receive those two in the following format - Insurance type(s): Commercial lines, Life Insurance.
Please have a look at this site: http://thaba-bosiu.co.ls.www31.jnb1.host-h.net/?page_id=46
There are multiple check boxes but the provider will receive the selected options. That is what I want.
 
PHP has a design flaw that overwrites previous field values if the same name is reused. The "name[]" is a workaround to tell it to instead put them all in an array. If you only want one value to be selected then radio buttons must be used. If you want multiple values to be possible you must use a loop to iterate through the array:
Code:
foreach($array as $value) {
   // do something with $value
}
or
Code:
foreach($array as &$value) {
   // do something with $value
}

unset($value)
 
PHP has a design flaw that overwrites previous field values if the same name is reused. The "name[]" is a workaround to tell it to instead put them all in an array. If you only want one value to be selected then radio buttons must be used. If you want multiple values to be possible you must use a loop to iterate through the array:
Code:
foreach($array as $value) {
   // do something with $value
}
or
Code:
foreach($array as &$value) {
   // do something with $value
}

unset($value)

what??
 
Suppose a web visitor selects Commercial and Life insurance, I want to receive those two in the following format - Insurance type(s): Commercial lines, Life Insurance.
Please have a look at this site: http://thaba-bosiu.co.ls.www31.jnb1.host-h.net/?page_id=46
There are multiple check boxes but the provider will receive the selected options. That is what I want.

Then my suggestion in post 4 should work. Does it not?
 
You suggest that I - "First, name each checkbox differently. Second, use an array".
1. Each checkbox has been named differently. please correct me if I am wrong and elaborate.
2. I need help with getting the array to work correctly with the differently named checkboxes.
 
*solved*
Thank you very much GreGorGy, I owe you a beer.
 
Top
Sign up to the MyBroadband newsletter
X