PHP and form arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roonui
Forum Newbie
Posts: 6
Joined: Fri Jun 08, 2012 9:15 pm

PHP and form arrays

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP and form arrays

Post 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']
roonui
Forum Newbie
Posts: 6
Joined: Fri Jun 08, 2012 9:15 pm

Re: PHP and form arrays

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP and form arrays

Post 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.
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP and form arrays

Post by requinix »

What's in formstep4?

Code: Select all

print_r($_SESSION['formstep4']);
roonui
Forum Newbie
Posts: 6
Joined: Fri Jun 08, 2012 9:15 pm

Re: PHP and form arrays

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP and form arrays

Post by requinix »

You need it in the HTML but not in the PHP.
roonui
Forum Newbie
Posts: 6
Joined: Fri Jun 08, 2012 9:15 pm

Re: PHP and form arrays

Post by roonui »

Still only gives me the first one:

Array ( [health[]] => alzheimer [fm-meds] => [fm-acc] => [fm-allergies] => [fm-diseases] => [fm-injurydetails] => [fm-alldetails] => )
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP and form arrays

Post by pickle »

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.
roonui
Forum Newbie
Posts: 6
Joined: Fri Jun 08, 2012 9:15 pm

Re: PHP and form arrays

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP and form arrays

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
roonui
Forum Newbie
Posts: 6
Joined: Fri Jun 08, 2012 9:15 pm

Re: PHP and form arrays

Post 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?
Post Reply