开发者

looping through a set of radio buttons and find if its empty in PHP

开发者 https://www.devze.com 2022-12-14 09:11 出处:网络
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 o

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" valu开发者_开发知识库e="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.


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.


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
}


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
      }

?>


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号