Web Hosting Talk







View Full Version : PHP: Playing with CheckBox ?


10gbus
09-22-2009, 09:23 AM
Hi

I have 2 checkbox fields

<input type="checkbox" name="music[1]"/>
<input type="checkbox" name="music[2]"/>


So when the user tick on those 2 checkboxes and click submit button
the address bar is

order.php?step=1&music[1]=on&music[2]=on

My question is

How can I get the values [1] and [2] using PHP ?

Means, I just want an array with those values, 1 and 2

Any help ?

SC-Daniel
09-22-2009, 09:46 AM
It sounds like you are using GET instead of POST -- by using POST the values would not show up in the address bar but would transfer to the script you specify.

In order.php you would access the posted variables by using $_POST[music[1]]

Just be sure to sanitize the values of the posted data if you are going to be doing anything with it and a database -- mysql_real_escape_string() -- never trust user input ;)

mattle
09-22-2009, 01:21 PM
Um...actually, you can go either way (or both) *"your mom" joke here*:

http://us2.php.net/manual/en/reserved.variables.get.php
http://us2.php.net/manual/en/reserved.variables.post.php
http://us2.php.net/manual/en/reserved.variables.request.php

I'm more curious about the chosen form field names from the original poster. The array syntax is usually reserved for checkbox groups, ie:


<input type="checkbox" name="music[]" value="1">
<input type="checkbox" name="music[]" value="2">

// and then check with
print_r($_REQUEST['music']);