user382538
user382538

Reputation:

html radio button array

I have an HTML form with radio buttons in a loop with same name like this:

Post Id 1:<input type="radio" name="radiob[]" id="radio" value="Yes" />
Post Id 2:<input type="radio" name="radiob[]" id="radio" value="Yes" />

I want to save radio button selected post into database but I want the user to select only one post. When I put post id with radio button name like radiob[2], the user can select multiple radio buttons so how can the user only check one radio button and the form send both the radio button id and value?

Thanks.

Upvotes: 0

Views: 10948

Answers (2)

Felix Kling
Felix Kling

Reputation: 816302

Use the ID as value, and you don't need to use radiob[] because only one value will be transmitted to the server anyway.

Post Id 1:<input type="radio" name="radiob" value="1" />
Post Id 2:<input type="radio" name="radiob" value="2" />

Upvotes: 1

Paul Norman
Paul Norman

Reputation: 1673

IDs should not be the same for 2 elements and the values should represent be what you need to store anyway:

<label for="radio_1">Post Id 1</label>:<input type="radio" name="radiob" id="radio_1" value="1" />
<label for="radio_2">Post Id 2</label>:<input type="radio" name="radiob" id="radio_2" value="2" />

You would then pick up the variables in php using either the get or post array (depending upon your submission method:

$value = $_POST['radiob']; // or $_GET['radiob']

Upvotes: 0

Related Questions