【发布时间】: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