Page 1 of 1

PHP and form arrays

Posted: Fri Jun 08, 2012 9:26 pm
by roonui
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!

Re: PHP and form arrays

Posted: Sat Jun 09, 2012 12:22 am
by requinix
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

Posted: Tue Jun 19, 2012 7:35 pm
by roonui
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?

Re: PHP and form arrays

Posted: Tue Jun 19, 2012 9:35 pm
by Christopher
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.

Re: PHP and form arrays

Posted: Tue Jun 19, 2012 9:35 pm
by requinix
What's in formstep4?

Code: Select all

print_r($_SESSION['formstep4']);

Re: PHP and form arrays

Posted: Tue Jun 19, 2012 10:34 pm
by roonui
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.

Re: PHP and form arrays

Posted: Wed Jun 20, 2012 12:41 am
by requinix
You need it in the HTML but not in the PHP.

Re: PHP and form arrays

Posted: Wed Jun 20, 2012 3:04 am
by roonui
Still only gives me the first one:

Array ( [health[]] => alzheimer [fm-meds] => [fm-acc] => [fm-allergies] => [fm-diseases] => [fm-injurydetails] => [fm-alldetails] => )

Re: PHP and form arrays

Posted: Wed Jun 20, 2012 10:00 am
by pickle
The problem might be in javascript, do a console.dir() of formData to see if the correct data is even being sent.

Re: PHP and form arrays

Posted: Fri Jun 22, 2012 4:26 pm
by roonui
Not too good with JS, here is the function:

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);
		});
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

Re: PHP and form arrays

Posted: Fri Jun 22, 2012 4:30 pm
by pickle
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.

Re: PHP and form arrays

Posted: Fri Jun 22, 2012 6:19 pm
by roonui
This is all the data that is being sent from form4:

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=
%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?