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 o开发者_JS百科nly check one radio button and the form send both the radio button id and value?
Thanks.
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" />
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']
精彩评论