2015-10-19 90 views
1

目标是创建四个内联单选按钮,并带有一个按钮将该表单提交给一个简单的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 */ 
} 
?> 
+1

单选按钮必须具有相同的名称是一伙的,你需要什么做的是改变每个电台的价值,并在PHP中进行比较。 '$ _POST ['my_radio'] =='google'''$ _POST ['my_radio'] =='yahoo'' ... –

+0

为什么你会为同一个广播组保留不同的名字?将名称更改为“大小”,然后将值分别更改为“小,中,大”。然后在你的php'$ _POST ['size']'将有字符串大小。 –

回答

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

你有一点点错误。单选按钮必须有一个共同的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"); 
} 

选择的值...等