PHP表单复选框

2018-02-22 16:40 更新

PHP教程 - PHP表单复选框

复选框字段是一个简单的切换按钮。它可以是开或关。

value属性应该包含在选中复选框时将发送到服务器的值。如果未选中复选框,则不会发送任何内容。

<label for="checkboxField">A checkbox field</label> 
<input type="checkbox" name="checkboxField" id="checkboxField" value="yes" /> 

您可以通过向输入标记添加checked =“checked"属性来预先选择复选框:

<input type="checkbox" checked="checked" ... />. 

通过创建具有相同name属性的多个复选框字段,您可以允许用户选择同一字段的多个值。



例子

以下脚本用于index.htm。 它有几个复选框。

<html>
<body>
<form action ="index.php">

<ul>
  <li><input type ="checkbox" name ="chkFries" value ="11.00">Fries</li>
  <li><input type ="checkbox" name ="chkSoda"  value ="12.85">Soda</li>
  <li><input type ="checkbox" name ="chkShake" value ="1.30">Shake</li>
  <li><input type ="checkbox" name ="chkKetchup" value =".05">Ketchup</li>
</ul>
<input type ="submit">
</form>

</body>
</html>

以下代码用于index.php,它接受来自复选框的值。

<?PHP
print "chkFries:" .  $chkFries . "<br/>";
print "chkSoda:" $chkSoda . "<br/>";
print "chkShake:" . $chkShake . "<br/>";
print "chkKetchup" . $chkKetchup . "<br/>";

$total = 0;

if (!empty($chkFries)){
  print ("You chose Fries <br>");
  $total = $total + $chkFries;
}

if (!empty($chkSoda)){
  print ("You chose Soda <br>");
  $total = $total + $chkSoda;
}

if (!empty($chkShake)){
  print ("You chose Shake <br>");
  $total = $total + $chkShake;
}

if (!empty($chkKetchup)){
  print ("You chose Ketchup <br>");
  $total = $total + $chkKetchup;
}

print "The total cost is \$$total";

?>


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号