codecamp

PHP表单复选框

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

?>


PHP表单Textarea
PHP表单选择
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Operator

Introduction

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }