duansang8388 2015-10-19 15:14
浏览 127
已采纳

更改单选按钮上的名称值(引导程序)

The goal is to create four inline radio buttons, with a button to submit the form to a simple php script. The problem is that changing the name attribute from 'optradio' to something like 'small' breaks the switching between buttons.

The html:

    <form action="logo-tshirt.php" method="post">
        <label class="radio-inline"><input type="radio" name="small" value="1">S</label>
        <label class="radio-inline"><input type="radio" checked name="medium" value="1">M</label>
        <label class="radio-inline"><input type="radio" name="large" value="1">L</label>
        <label class="radio-inline"><input type="radio" name="extra-large" value="1">XL</label><br />
        <input style="margin-top:5px" type="submit" value="Buy With PayPal" name="send" class="btn btn-primary">
    </form>

The php:

<?php
if($_POST['small'] == 1 ){ 
    header("Location: http://www.google.com"); 
} 
elseif($_POST['medium'] == 1 ){ 
    header("Location: http://www.yahoo.com"); 
} 
elseif($_POST['large'] == 1 ){ 
    header("Location: http://www.bing.com"); 
} 
elseif($_POST['extra-large'] == 1 ){ 
    header("Location: http://www.bbc.co.uk"); 
} 
else { 
    header("Location: http://www.theguardian.co.uk"); 
} 
?>

Thanks for all the answers. The one i used was AI.G's because it used the least lines of code. This is my working code.

<form action="logo-tshirt.php" method="post">
    <label class="radio-inline"><input type="radio" name="optradio" value="small">S</label>
    <label class="radio-inline"><input checked type="radio" name="optradio" value="medium">M</label>
    <label class="radio-inline"><input type="radio" name="optradio" value="large">L</label>
    <label class="radio-inline"><input type="radio" name="optradio" value="extra-large">XL</label><br />
    <input style="margin-top:5px" type="submit" value="Buy With PayPal" name="send" class="btn btn-primary">
</form>

The php:

<?php
switch ($_POST['optradio']) {
    case "small": header("Location: http://www.google.com"); break;
    case "medium": header("Location: http://www.yahoo.com"); break;
    case "large": header("Location: http://www.bing.com"); break;
    case "extra-large": header("Location: http://www.bbc.co.uk"); break;
    default:header("Location: http://www.theguardian.co.uk");   /* if $_POST['option'] was none of the above */
}
?>
  • 写回答

2条回答 默认 最新

  • douliao5942 2015-10-19 15:19
    关注

    All radios should have the same name but different values:

    <label class="radio-inline"><input type="radio" name="option" value="small">S</label>
    <label class="radio-inline"><input type="radio" checked name="option" value="medium">M</label>
    <label class="radio-inline"><input type="radio" name="option" value="large">L</label>
    <label class="radio-inline"><input type="radio" name="option" value="extra-large">XL</label>
    

    Now check them using a switch:

    switch ($_POST['option']) {
        case "small": /* redirect to google */ break;
        case "medium": /* redirect to yahoo */ break;
        /* etc... */
        default:
            /* if $_POST['option'] was none of the above */
            /* redirect to somewhere else */
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法