0

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.

1
  • 5
    You do know that duplicate id values violates the specs, right? Commented Dec 16, 2010 at 16:52

2 Answers 2

1

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" />
Sign up to request clarification or add additional context in comments.

3 Comments

Sending the same value for a bunch of radios makes no sense - it's missing the point of having a radio. This is a much better way.
what i am trying to do is saving in db that this selected this post so in post id feild i save post id and in text feild i save yes
@livetolearn: Well I don't know the overall structure of your form. Maybe you should post it and precisely explain how it works.
0

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']

Comments

Your Answer

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