tags:

views:

260

answers:

4

my question is that i have a group of radio buttons and all are grouped into one 'x0'.Now how do i iterate through this radiobutton group using foreach ,and find if its empty/or not and do further operations based on the value?

 <tr>
       <td><input type="radio"  name="x0" value="0" <?=$x0?>> 0. </td>
  </tr>
 <tr>
 <td><input type="radio" name="x0" value="1" <?=$x1?>> 1. </td>
 </tr>
 <tr>
 <td><input type="radio" name="x0" value="2" <?=$x2?>> 2. </td>
 </tr>
 <tr>
 <td><input type="radio" name="x0" value="3" <?=$x3?>> 3. </td>  
 </tr>

Thanks in advance.

+7  A: 

Remember, a radio button can only have one option selected. You don't need to loop, POST['X0'] will by equal whatever number was selected.

MindStalker
A: 

HTML Variable Array

<td><input type="radio"  name="x0[]" value="0" <?=$x0?>> 0. </td>


<?php 

      //FORM ACTION == POST

      var_dump($_POST['x0']); //to see submitted radio buttons

      foreach($_POST['x0'] as $index=>$radio) { 
           //iterate through the radio buttons
      }

?>
andreas
+1  A: 

PHP is not going to be able to directly access the html elements. If you're looking for operating on it after submission, the previous answers will help. If you mean on the page, then I think you're looking for javascript, in which case this would be it (excuse my poor JS skills:

var buttons = document.getElementsByName("x0");
for (var i = 0; i < buttons.length; i++)
{
    // do something with buttons[i].checked
}
Tesserex
A: 

When using radio buttons in html you assigned the same name to a group of radios, that are mutually exclusive. The browser will only post the selected value. No matter what language you're using at the server side, you only need to ask for the value associated with "x0" (from your example)

Dani Cricco