doushi1960 2013-04-11 08:25
浏览 12

在后阵列中没有正确地获得单选按钮

I have block of code which is loaded while clicking a particular button through Ajax.

I have given each input a name like txtname[], country[] and also radiobtn[].

The problem is, when I am filling these 3 fields twice and selecting a radio button in the second iteration and then printing post array through print_r($_POST);

is showing the following results below:

[txtname] => Array ( [0] => xyz[1] => abc)[country] => Array ( [0] => India[1] => United Kingdom ) [radiobtn] => Array ( [0] => true )

It shouldn't show like:

[txtname] => Array ( [0] => xyz[1] => abc)[country] => Array ( [0] => India[1] => United Kingdom ) [radiobtn] => Array ( [0] => ""[1] => true)

So how to go about knowing which iteration radio button is selected? Any help appreciated.

HTML

<input type="radio"  name="radiobtn[]" value="true">
<input  type="text" name="txtname[]" >
   <select name="country[]"> 
<option value="country" selected="selected">Select Country</option> 
<option value="United States" >India </option> 
<option value="United Kingdom">United Kingdom</option> 
   </select>
  • 写回答

1条回答 默认 最新

  • dongyirong3564 2013-04-11 13:18
    关注

    You are expecting two values from radiobtn which is of type radio. The radio button type is supposed to force the selection of only one item, therefore the radiobtn[] array you are trying to populate will always contain only one value, which is true. And that is correctly shown by the result:

    [radiobtn] => Array ( [0] => true )
    

    Your radiobtn[] array is overwritten at each iteration, leading to only the last result being stored in the $_POST global variable. Therefore the [] in radiobtn[] is pointless since there will never be more than one value.

    For multiple selections you may want to take a look at type="checkbox" which can store multiple values for the same name. An example would be:

    <input type="checkbox" name="btn[]" value="A">A</input>
    <input type="checkbox" name="btn[]" value="B">B</input>
    <input type="checkbox" name="btn[]" value="C">C</input>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真