php checkbox problems

September 22, 2010 at 02:11:25
Specs: Windows 7
I have trawled the web trying to find something that works but nothing as yet, i have a questionnaire that i made using forms in dreamweaver, i then found some php code on a youTube that allows me to gather the results and post them to email.
the problem i have is yes great it posted them to my email but the checkboxes question would only display 1 answer, i later found a result on this forum which talked about making it an array which i followed the code and it detects it as an array but again only shows 1 result in the email, so i will post my code and can anyone help??? (also cannot get it to display who the email is from if anyone has any ideas?

PHP Code:


/* Email and subject line variables */

$emailSubject = 'Pharmacy Monitoring';
$emailResults = '';

/* Gathering results variables */

$nameField = $_POST['name'];
$jobField = $_POST['jobTitle'];
$q1Field = $_POST['Q1'];
$q2Field = $_POST['Q2'];
foreach ($q2Field as $q2Fieldname)
$q3Field = $_POST['Q3'];
$q4Field = $_POST['Q4'];
$q5Field = $_POST['Q5'];
$q6Field = $_POST['Q6'];
$q7Field = $_POST['Q7'];
$q8Field = $_POST['Q8'];
$q9Field = $_POST['Q9'];
$q10Field = $_POST['Q10'];
$q11Field = $_POST['Q11'];
$q12Field = $_POST['Q12'];

$body = <<<EDO


Name: $nameField

Job Title: $jobField

Question 1: $q1Field

Question 2: $q2Fieldname

Question 3: $q3Field

Question 4: $q4Field

Question 5: $q5Field

Question 6: $q6Field

Question 7: $q7Field

Question 8: $q8Field

Question 9: $q9Field

Question 10: $q10Field

Question 11: $q11Field

Question 12: $q12Field


$headers = "From: $name";
$headers = "Content-type: text/html\r\n";
$success = mail ($emailResults, $emailSubject, $body, $headers);

header ("location: contents.html");


HTML looks like this for the question:

<input type="checkbox" name="Q2[]" value="Storage of Medicine - Correct" id="Question2_0" />
Storage of Medicine</label>
<br />
<input type="checkbox" name="Q2[]" value="Allergy information - Wrong" id="Question2_1" />
Allergy information</label>
<br />
<input type="checkbox" name="Q2[]" value="What to do if there is a drug error - Correct" id="Question2_2" />
What to do if there is a drug error</label>
<br />
<input type="checkbox" name="Q2[]" value="Required frequency of medication - Wrong" id="Question2_3" />
Required frequency of medication</label>
<br />

Email Results:

Name: aaaaa
Job Title:
Question 1:
Question 2: What to do if there is a drug error - Correct
Question 3:
Question 4:
Question 5:
Question 6:
Question 7:
Question 8:
Question 9:
Question 10:
Question 11:
Question 12:

See More: php checkbox problems

Report •

September 22, 2010 at 12:03:05
It *may* be easier to use the radio button here since a user can leave both checkboxes for a given answer blank, which would throw off your form processing if you aren't checking for it. You could use javascript to make sure a choice is made, but a user could have scripting support disabled.

Here's a quick snippit using radio buttons that *may* help.


   $answers = array("q1" => array("Question 1: ", "Yes"), 
                    "q2" => array("Question 2: ", "No"),
                    "q3" => array("Question 3: ", "Yes"),
                    "q4" => array("Question 4: ", "No"),
                    "q5" => array("Question 5: ", "No"));

   if(isset($_POST["cmd"]) && $_POST["cmd"] === "Submit") {

      foreach($_POST as $key => $val) {
         if($key !== "cmd") {
         echo $answers[$key][0] . "answered (" . $val . "); answer [" . $answers[$key][1] . "]" . 
              ($answers[$key][1] === $val ? " correct" : " incorrect") . "<br />";

      echo "<hr align='left' width='25%'>";
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
Question 1: Yes<input name="q1" type="radio" value="Yes" checked>No<input name="q1" type="radio" value="No"><br />
Question 2: Yes<input name="q2" type="radio" value="Yes" checked>No<input name="q2" type="radio" value="No"><br />
Question 3: Yes<input name="q3" type="radio" value="Yes" checked>No<input name="q3" type="radio" value="No"><br />
Question 4: Yes<input name="q4" type="radio" value="Yes" checked>No<input name="q4" type="radio" value="No"><br />
Question 5: Yes<input name="q5" type="radio" value="Yes" checked>No<input name="q5" type="radio" value="No"><br />
<input name="cmd" value="Submit" type="submit">


I have no aspiration for preaching. ;-)

Report •

September 22, 2010 at 23:47:26
Hi, thanks for taking sometime to reply to my posting but maybe i didnt make it clear when i posted the email results. i get answers for all of the questions but question 2 i made checkboxes because i want to receive more than 1 result.

example of email results:
Name: Michael
Job Title: ICT Network Manager
Question 1: Folder 1 - Correct
Question 2: Required frequency of medication - Wrong
Question 3: A - Correct
Question 4: Topical - Correct
Question 5: Can be bought in chemists or supermarkets - Correct
Question 6: Clients date of birth - Correct
Question 7: mg - Correct
Question 8: To be given in the evening - Wrong
Question 9: Inform the school nurse or senior member of staff at once - Correct
Question 10: The medication is kept in the drug cupboard but the student administers the medication and signs the medication administration chart - Correct
Question 11: Locked away in the inner cupboard of the drugs cupboard - Correct
Question 12: Once a week - Correct

I checked 2 check boxes for question 2 but it only sends the last answer checked and not both answers that have been checked..... any idea's???

Report •

September 23, 2010 at 01:17:46
The foreach in here doesn't make sense:
$q2Field = $_POST['Q2'];
foreach ($q2Field as $q2Fieldname)
$q3Field = $_POST['Q3'];

You loop through each checked item, but don't do anything with it.
So, $q2Fieldname is set to the last value in the Array at the end of the loop...

Instead of a foreach, try something like:

$q2Field = implode(', ',$_POST['Q2']);

Should give you a comma separated list of what was selected for Q2.

Report •

Related Solutions

September 23, 2010 at 01:25:53
thanks fmwap that works a treat.....
does anyone know why the from part of the script doesnt work the emails always say received from Nobody ???

Report •

September 23, 2010 at 05:51:16
The best I can tell is $headers is being overwritten by "Content-type: text/html\r\n" instead of being appended to the end of "From: $name".

Did you define $name?

I have no aspiration for preaching. ;-)

Report •

September 27, 2010 at 02:24:28
thanks Shutat, but that still doesnt help me out i'm almost an absolute beginner at php does anyone out there see where i have gone wrong with the From: command??

Report •

September 27, 2010 at 06:13:06
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: who_is_receiving_name <" . $their_email_address . ">\r\n";
$headers .= "From: being_sent_from_name <" . $your_email_address . ">\r\n";

$result = mail($their_email_address, $subject, $message, $headers);

I don't usually have any problem using something like the above.

In your code, you've got $nameField = $_POST["name"], but I didn't notice anywhere just $name being assigned a value. Try $nameField instead of $name.


I have no aspiration for preaching. ;-)

Report •

September 27, 2010 at 07:34:49
Hi Shutat,
$nameField and the removal of content type worked it now lets me know who sent the email using the name field.

thanks again for all your help


Report •

Ask Question