PHP and form arrays
Posted: Fri Jun 08, 2012 9:26 pm
Hello, I am currently working on a form where a fairly large amount of data is to be filled out by a user, and the information is then emailed back to me. The form makes use of a few checkboxes, here is a small sample of some:
<input style="width:20px;" type="checkbox" name="health[]" value="asthma" />Asthma<br />
<input style="width:20px;" type="checkbox" name="health[]" value="alzheimer" />Alzheimers<br />
<input style="width:20px;" type="checkbox" name="health[]" value="arthritis" />Arthritis<br />
<input style="width:20px;" type="checkbox" name="health[]" value="back" />Back Problems<br />
<input style="width:20px;" type="checkbox" name="health[]" value="blackouts" />Blackouts<br />
<input style="width:20px;" type="checkbox" name="health[]" value="op" />Booked for operation<br />
<input style="width:20px;" type="checkbox" name="health[]" value="cancer" />Cancer<br /><br />
As you can see, they all have the name "health[]". This form is serialized with javascript and sent to process as follows:
var formData = $("#form_"+curr).serialize();
$.get('processForm.php', {saveData: curr, formData: formData, step : 'getAllData'}, function(data) {
$("#stepTable tr").removeClass('stepSelected');
$(".multiform_part").html(data);
});
All fine, until we get to processForm.php. All other inputs can be emailed by tacking on to a string:
$msg .= "Reference name: ".$_SESSION['formstep3']['fm-ref1name']."<br>\n";
And sending that off, but when I get to the checkboxes, it only gives one of the checked options, regardless of how many I check. I have tried several variations on the following code:
foreach ($_SESSION['formstep4']['health[]'] as $value) {
$msg .= "Health: ".$value."<br>\n";
}
as well as the standard ones that are given through google searches. Perhaps someone could show me the light on how to get those checked health values appended to my email?
Cheers!
<input style="width:20px;" type="checkbox" name="health[]" value="asthma" />Asthma<br />
<input style="width:20px;" type="checkbox" name="health[]" value="alzheimer" />Alzheimers<br />
<input style="width:20px;" type="checkbox" name="health[]" value="arthritis" />Arthritis<br />
<input style="width:20px;" type="checkbox" name="health[]" value="back" />Back Problems<br />
<input style="width:20px;" type="checkbox" name="health[]" value="blackouts" />Blackouts<br />
<input style="width:20px;" type="checkbox" name="health[]" value="op" />Booked for operation<br />
<input style="width:20px;" type="checkbox" name="health[]" value="cancer" />Cancer<br /><br />
As you can see, they all have the name "health[]". This form is serialized with javascript and sent to process as follows:
var formData = $("#form_"+curr).serialize();
$.get('processForm.php', {saveData: curr, formData: formData, step : 'getAllData'}, function(data) {
$("#stepTable tr").removeClass('stepSelected');
$(".multiform_part").html(data);
});
All fine, until we get to processForm.php. All other inputs can be emailed by tacking on to a string:
$msg .= "Reference name: ".$_SESSION['formstep3']['fm-ref1name']."<br>\n";
And sending that off, but when I get to the checkboxes, it only gives one of the checked options, regardless of how many I check. I have tried several variations on the following code:
foreach ($_SESSION['formstep4']['health[]'] as $value) {
$msg .= "Health: ".$value."<br>\n";
}
as well as the standard ones that are given through google searches. Perhaps someone could show me the light on how to get those checked health values appended to my email?
Cheers!