ThePixelPony
ThePixelPony

Reputation: 741

Submitting HTML Form Values as Array (To PHP)

I've got a lot of radio boxes on a single page within my website.

I know it is possible to submit all of the options as an array that can be directly manipulated by PHP.

<input type="radio" name="SOMETHING_HERE" value="1" />

There are several radio groups and I'd like all of them to submit to one array.

All I'd like to know is the syntax which has to be used in the name="".

Upvotes: 1

Views: 79

Answers (3)

Santosh Achari
Santosh Achari

Reputation: 3006

You can have your form in either of the following ways.

<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />

Or

<input type="radio" name="question['question1']" value="1" />
<input type="radio" name="question['question2']" value="2" />

There by you setting the array definition yourself. Hope this answers your query.

Upvotes: 1

deed02392
deed02392

Reputation: 5022

HTML looks like this:

<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />

PHP accesses it like this:

<?php
$_POST['name'] = array('1','2');

Some things to note:

  • Order in array as compared to as it exists in HTML is not guaranteed

Upvotes: 0

Rakesh Shetty
Rakesh Shetty

Reputation: 4568

Use like this

<input type="radio" name="optionname[]" value="1" />
<input type="radio" name="optionname[]" value="2" />
.
.

Upvotes: 3

Related Questions