PHP and form arrays
Moderator: General Moderators
PHP and form arrays
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!
Re: PHP and form arrays
Leave off the [] when you access it in your code. That's just there to signal to PHP that there are multiple values (ie, it's an array).
Code: Select all
$_SESSION['formstep4']['health']Re: PHP and form arrays
Ok so I changed it as you said:
foreach ($_SESSION['formstep4']['health'] as $value) {
$msg .= "Health: ".$value."<br>\n";
}
I checked 3 of the options in the health section, but it does not display anything in my email. In fact, it does not even display the word Health, so it thinks there is nothing in the array. Is it that I am not using $value properly? Should it be $value[x] or something?
foreach ($_SESSION['formstep4']['health'] as $value) {
$msg .= "Health: ".$value."<br>\n";
}
I checked 3 of the options in the health section, but it does not display anything in my email. In fact, it does not even display the word Health, so it thinks there is nothing in the array. Is it that I am not using $value properly? Should it be $value[x] or something?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP and form arrays
I don't think you need the [] at all, since you are serializing the data with Javascript. That notation is only if PHP is processing the $_POST data.
(#10850)
Re: PHP and form arrays
What's in formstep4?
Code: Select all
print_r($_SESSION['formstep4']);Re: PHP and form arrays
Took out the [] from the html form, and it only prints out the first option I clicked.
print_r =
Array ( [health] => blackouts [fm-meds] => xcxcxc [fm-acc] => [fm-allergies] => [fm-diseases] => [fm-injurydetails] => [fm-alldetails] => )
I am using this thing called multiform.php, a script written by someone else that I am modifying. It uses javascript to present the user with different forms when they click on sections to complete, without actually submitting anything. Then it is all submitted at the same time one all forms are complete. All works well, except handling these checkboxes, and radio buttons.
print_r =
Array ( [health] => blackouts [fm-meds] => xcxcxc [fm-acc] => [fm-allergies] => [fm-diseases] => [fm-injurydetails] => [fm-alldetails] => )
I am using this thing called multiform.php, a script written by someone else that I am modifying. It uses javascript to present the user with different forms when they click on sections to complete, without actually submitting anything. Then it is all submitted at the same time one all forms are complete. All works well, except handling these checkboxes, and radio buttons.
Re: PHP and form arrays
You need it in the HTML but not in the PHP.
Re: PHP and form arrays
Still only gives me the first one:
Array ( [health[]] => alzheimer [fm-meds] => [fm-acc] => [fm-allergies] => [fm-diseases] => [fm-injurydetails] => [fm-alldetails] => )
Array ( [health[]] => alzheimer [fm-meds] => [fm-acc] => [fm-allergies] => [fm-diseases] => [fm-injurydetails] => [fm-alldetails] => )
Re: PHP and form arrays
The problem might be in javascript, do a console.dir() of formData to see if the correct data is even being sent.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: PHP and form arrays
Not too good with JS, here is the function:
Where would I put the console.dir()? I assume its console.dir(formData); and then use Firebug to view? If so, I cant seem to get any information in the firebug window from console.dir
Code: Select all
$("#previewBtn").click(function(e){
e.preventDefault();
$(this).hide();
$("#prevStepBtn").hide();
var curr = $(".stepSelected").children(':eq(1)').children().attr('id');
var formData = $("#form_"+curr).serialize();
$.get('processForm.php', {saveData: curr, formData: formData, step : 'getAllData'}, function(data) {
$("#stepTable tr").removeClass('stepSelected');
$(".multiform_part").html(data);
});Re: PHP and form arrays
Right after you declare formData.
If Firebug isn't showing anything, restart Firefox - sometimes after it's been open for a while, Firebug start acting flakey. If console.dir() still isn't showing anything, try changing it to an alert(). If that still doesn't happen, then the code isn't being run.
Also, be sure to post your code in appropriate [syntax][/syntax] tags. I've done that for you here.
If Firebug isn't showing anything, restart Firefox - sometimes after it's been open for a while, Firebug start acting flakey. If console.dir() still isn't showing anything, try changing it to an alert(). If that still doesn't happen, then the code isn't being run.
Also, be sure to post your code in appropriate [syntax][/syntax] tags. I've done that for you here.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: PHP and form arrays
This is all the data that is being sent from form4:
%5B%5D doesnt look right? shouldnt it leave out the [] or in this case the ascii codes for them? I am pretty sure this is now a JS issue. I know this is a PHP forum, but any ideas on how to fix that?
Code: Select all
health%5B%5D=asthma&health%5B%5D=alzheimer&health%5B%5D=arthritis&fm-meds=&fm-acc=&fm-allergies=&fm-diseases=&fm-injurydetails=&fm-alldetails=