By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,406 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,406 IT Pros & Developers. It's quick & easy.

PHP array from HTML form checkbox or select

P: n/a
RC
Order for PHP get the array from a
HTML form checkbox or select tag.
The HTML name MUST with [].
For example:

<form methos=POST action="myfile.php">
<select name="pets[]" multiple>
<option value="cat">Cat
<option value="dog">Dog
....
</select>

Or
<input type="checkbox" name="fruits[]" value="apple">
<input type="checkbox" name="fuits[]" value="orange">

</form>

But the problem is name="XXX[]" is not standard HTML, right?
Especial when my HTML file has JavaScript, JavaScript
treats the name of select or checkbox tag as an array,
so it doesn't recognized name with []. In JavaScript treats

form.pets[0].value = "cat"
Can't treats
form.pets[][0].value = "cat"

But in PHP

$pets = $_POST['pets'];
$fruits = $_POST['fruits'];

How can I solve this problem a HTML form has JavaScript and
process with PHP?

Thank Q very much in advance!

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
RC <ra**********@nospam.noaa.gov> wrote:
But the problem is name="XXX[]" is not standard HTML, right?
No, it's valid HTML according to w3c specs:
http://www.w3.org/TR/html401/interac...def-name-INPUT
Especial when my HTML file has JavaScript, JavaScript
treats the name of select or checkbox tag as an array,
so it doesn't recognized name with []. In JavaScript treats

form.pets[0].value = "cat"
Can't treats
form.pets[][0].value = "cat"


That's because you are using [] in it's javascript context (array) while
instead you should be using a literal []: form["pets[]"].value

So readup on your javascript skills :)

Jul 17 '05 #2

P: n/a
RC wrote:
Order for PHP get the array from a
HTML form checkbox or select tag.
The HTML name MUST with [].
For example:

<form methos=POST action="myfile.php">
<select name="pets[]" multiple>
<option value="cat">Cat
<option value="dog">Dog
...
</select>

Or
<input type="checkbox" name="fruits[]" value="apple">
<input type="checkbox" name="fuits[]" value="orange">

</form>

But the problem is name="XXX[]" is not standard HTML, right?
Especial when my HTML file has JavaScript, JavaScript
treats the name of select or checkbox tag as an array,
so it doesn't recognized name with []. In JavaScript treats

form.pets[0].value = "cat"
Can't treats
form.pets[][0].value = "cat"

But in PHP

$pets = $_POST['pets'];
$fruits = $_POST['fruits'];

How can I solve this problem a HTML form has JavaScript and
process with PHP?

Thank Q very much in advance!


Use the id= field to give each field a unique id. You can then
reference the field by id in your Javascript and still access the array
in your PHP.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.