duanpei8518 2013-04-19 01:58
浏览 21

单选按钮上的自动增量名称属性

I have a form that has a set of radio buttons, and I am sending the value of the checked button to another page with php. The form has the option to add another set of the same radio buttons, so if I use the following method of giving name attributes:

<div>
    <input type="radio" name="food[]" value="apple" /> Apple
    <input type="radio" name="food[]" value="orange" /> Orange
</div>

And add a clone of the above,

$_POST['food'];

This would give me the value of each input, and that would total 4. I'm trying to use the bracket incrementing method but still keep the functionality of returning the value of the radio button that was checked.

So my question is, is there a way to use the square brackets to do auto incrementing on the name attribute, but when sending the values to php make sure each div grouping of radio buttons only returns one of the values for whichever one is checked? Please let me know if this question is too vague, this is my first question here.

  • 写回答

2条回答 默认 最新

  • doujing2017 2013-04-19 13:41
    关注

    May i don't understand your question. You can not Auto Incremented the name cause you use radio buttons. By Choosing apple on the first page and orange on the second you will loose your first choose (apple). You can do it with checkboxes instead of radio buttons:

    Page 1:

    <form method="post" action="page2.php">
    <div>
        <input type="checkbox" name="numberofapples" value="1" /> Apple
        <input type="checkbox" name="numberoforanges" value="1" /> Orange
    </div>
    <input type="submit" value="go">
    </form>
    

    Page 2:

    <form method="post" action="result.php">
    <div>
        <?if($_POST['numberofapples']){?><input type="hidden" name="numberofapples" value="1" /> <?}?>
        <input type="checkbox" name="numberofapples" value="<?=($_POST['numberofapples'])?2:1?>" /> Apple
        <?if($_POST['numberoforanges']){?><input type="hidden" name="numberoforanges" value="1" /> <?}?>
        <input type="checkbox" name="numberoforanges" value="<?=($_POST['numberoforanges'])?2:1?>" /> Orange
    </div>
    <input type="submit" value="go">
    </form>
    

    Result page:

    Apples: <?=($_POST['numberofapples'])?$_POST['numberofapples']:0?>
    Oranges: <?=($_POST['numberoforanges'])?$_POST['numberoforanges']:0?>
    

    But why don't you do the counting after posting your second page? (add the post values from page 1 as hidden fields to the form of page 2)

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。