【问题标题】:Changing name value on radio button (bootstrap)更改单选按钮上的名称值(引导程序)
【发布时间】:2015-10-19 23:39:03
【问题描述】:

我们的目标是创建四个内联单选按钮,用一个按钮将表单提交给一个简单的 php 脚本。问题在于将名称属性从“optradio”更改为“small”会破坏按钮之间的切换。

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>

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"); 
} 
?>

感谢您的所有回答。我使用的是 AI.G,因为它使用的代码行数最少。这是我的工作代码。

<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>

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 */
}
?>

【问题讨论】:

  • 单选按钮需要有相同的名称才能成为一个组,您需要做的是更改每个单选的值并在PHP中进行比较。 $_POST['my_radio'] == 'google'$_POST['my_radio'] == 'yahoo' ...
  • 为什么要为同一个电台组保留不同的名称?将名称更改为“大小”,然后将值分别更改为“小、中、大”。然后在你的 php $_POST['size'] 中会有大小字符串。

标签: php html twitter-bootstrap


【解决方案1】:

你说的有点不对。单选按钮必须有一个共同的name,它们的value 是实际发送到服务器的内容。示例:

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

现在您可以通过 $_POST 提取所选值

if ($_POST['buy'] == 'small' ){ 
    header("Location: http://www.google.com"); 
} 

...等

【讨论】:

    【解决方案2】:

    所有无线电应该具有相同的名称但不同的值:

    <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>
    

    现在使用开关检查它们:

    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 */
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2010-11-10
      • 1970-01-01
      相关资源
      最近更新 更多