[SOLVED] PHP and form processing, checkboxes...

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
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

[SOLVED] PHP and form processing, checkboxes...

Post by batfastad »

OK I'm pretty new to PHP and have been working on a form to e-mail script.
It all works fine apart from...

One of the fields on my form is a set of checkboxes.

The checkboxes have the same name name="foo" but each checkbox has a different value.

When I set the variable in my PHP script it just takes one of the values - the last one in the form.
If more than one of the checkboxes is checked, I would like it so that the variable gets all 3 separated by commas.

I know I could call each checkbox a different name, but I'm more curious as to how I would go about doing this.

The way I thought of doing it was by naming each checkbox differently, check1, check2, check3, then concatenating those three variables into another single variable.
I can cope with doing that.

I just wondered if there was another way to do this?

The old form to e-mail script I used to use was written in Perl, and when I set a checkbox field, with multiple options all with the same input name, the output came through as comma separated list of values.

Is the comma separated thing I saw in the results of the old Perl script something to do with the way data is transmitted via method=post
Or was it done using special scripting in Perl?

Would the solution I outlined above be the best way to solve this problem?

Could the data be parsed to an array of some kind when it is processed?
Doing something like, if a field name appears more than once, then put successive values into an array?

I'd be interested to hear your opinions.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

name="foo[]"
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

To elaborate, on ~feyd's comment, when you name a form element with square brackets, like an array, it is passed to PHP as an array.

Example:

Code: Select all

<?PHP

echo $_POST[foo][0];
echo $_POST[foo][1];
echo $_POST[foo][2];

echo <<<FORM
<form name = "foo_form" method = "POST" action = "$PHP_SELF">
<input type = "checkbox" value = "This" name = "foo[]">
<input type = "checkbox" value = "is" name = "foo[]">
<input type = "checkbox" value = "fun" name = "foo[]">
<input type = "submit" value = "Click To Submit" name = "submit">
</form>
FORM;
?>
When this form is submitted with all three checkboxes checked, you will see the output: "Thisisfun". Something to remember also is that a checkbox will only appear in the POST vars if it was checked.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

OK that all makes sense - wow that's so useful!!
pickle wrote:Something to remember also is that a checkbox will only appear in the POST vars if it was checked.
Why???
Is this the same for type="text" as well?
If it's unique to checkboxes (and/or radio buttons), does PHP look at the input types of the inputs from the form?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if a radio or checkbox isn't selected, the browser doesn't pass it.. there's nothing to pass..
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Just going back to your initial replies...

1) The form page sending the input is not PHP. But I could still have the input name as foo[] ? I'm thinking that I can as PHP will see it the same whether the form page is PHP or HTML.

2) Say I do that with the array, there are two ways I can get at the data from that form input - and from an array in general!!

Code: Select all

$foo0 = $_POST['foo[0]'];
$foo1 = $_POST['foo[1]'];
$foo2 = $_POST['foo[2]'];
Which will put each item in the array to a separate variable.

--OR--

$foo = $_POST['foo'];
Which will return the values concatenated as in your Thisisfun example above
Am I correct? Is this the same for all arrays - returning concatenated results when you call the variable name without the 'array item identifier'??

Thanks again guys for helping me through the learning process.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I don't know where they get dropped, all I know is that they are. I believe it's before PHP - perhaps when the client is processing form info to send?

Further to your comment,

Code: Select all

$foo = $_POST['foo'];
will NOT concatenate the values for you, it will assign the value of $_POST['foo'] (which is an array) to the variable $foo. You can then refer to the individual elements like so

Code: Select all

$foo[0];  // = $_POST[foo[0]] = "This"
$foo[1];  // = $_POST[foo[1]] = "is"
$foo[2];  // = $_POST[foo[2]] = "fun"
rather than needing to reference $_POST all the time.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Oh right, so

Code: Select all

$foo = $_POST['foo'];
is actually a shortcut to doing

Code: Select all

$foo0 = $_POST['foo[0]']; 
$foo1 = $_POST['foo[1]']; 
$foo2 = $_POST['foo[2]'];
or is it wrong/pointless to do that anyway - when you can just go $foo[1] or whatever.

I see!!

Thanks again guys
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Exactly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

I've had issues with check boxes before if I don't explicitly give it an index.

ie I've had to do this

Code: Select all

...
<form name = "foo_form" method = "POST" action = "$PHP_SELF">
<input type = "checkbox" value = "This" name = "foo[0]">
<input type = "checkbox" value = "is" name = "foo[1]">
<input type = "checkbox" value = "fun" name = "foo[2]">
<input type = "submit" value = "Click To Submit" name = "submit">
</form>
?>
If I don't and some of the check boxes are not selected they 'lose' there position in the array when posted back.... that was fun to track down.
Post Reply