1

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="".

3 Answers 3

3

Use like this

<input type="radio" name="optionname[]" value="1" />
<input type="radio" name="optionname[]" value="2" />
.
.
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Just to clarify, will the PHP array be in the following format: $_POST['question'] = array("question1" => 1, "question2" => 2)?
Yes. You can give it a try to see.
0

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

2 Comments

How it possible for PHP to access it like this: $_POST['answers'] = array("QuestionId" => Answer);?
By using value="foo" name="answers[QuestionId]" you would get $_POST['answers'] = array("QuestionId" => "foo");, but remember you must be careful to keep QuestionId unique for every input node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.