首頁 > php教程 > php手册 > 获取checkbox值的php代码

获取checkbox值的php代码

WBOY
發布: 2016-05-25 16:43:10
原創
1087 人瀏覽過

由于checkbox特殊性所以我们要获取它的值,在很我语言中都不一样的,在php中我们是以数组形式来处理,并且利用遍历他的值,下面有大量实例.

<html> 
	<head> 
	<title>checkbox demo</title> 
	</head> 
	<body> 
	<h1>checkbox demo</h1> 
	 
	<h3>demonstrates checkboxes</h3> 
	<form action ="handleformcheckbox.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> 
登入後複製

handleformcheckbox.php

 class="brush:php;"><html> 
	<head> 
	<title>checkbox demo</title> 
	</head> 
	<body> 
	<h3>demonstrates reading checkboxes</h3> 
	<?php 
	print <<<here 
	chkfries: $chkfries <br> 
	chksoda: $chksoda <br> 
	chkshake: $chkshake <br> 
	chkketchup: $chkketchup <br> 
	<hr> 
	 
	here; 
	 
	$total = 0; 
	 
	if (!emptyempty($chkfries)){ 
	  print ("you chose fries <br>"); 
	  $total = $total + $chkfries; 
	} 
	 
	if (!emptyempty($chksoda)){ 
	  print ("you chose soda <br>"); 
	  $total = $total + $chksoda; 
	} 
	 
	if (!emptyempty($chkshake)){ 
	  print ("you chose shake <br>"); 
	  $total = $total + $chkshake; 
	} 
	 
	if (!emptyempty($chkketchup)){ 
	  print ("you chose ketchup <br>"); 
	  $total = $total + $chkketchup; 
	} 
	 
	print "the total cost is $$total"; 
	 
	 
	</body> 
	</html> 
登入後複製

实例:

 class="brush:php;"><html> 
	<head> 
	    <title>using default checkbox values</title> 
	</head> 
	<body> 
	<?php 
	$food = $_get[food]; 
	$self = htmlentities($_server[&#39;php_self&#39;]); 
	if (!emptyempty($food)) { 
	    echo "the foods selected are:<br />"; 
	    foreach($food as $foodstuf) 
	    { 
	        echo "<strong>".htmlentities($foodstuf)."</strong><br />"; 
	    } 
	} 
	else 
	{ 
	    echo ("<form action="$self" "); 
	    echo (&#39;method="get"> 
	    <fieldset> 
	        <label>italian <input type="checkbox" name="food[]" value="italian" /> 
	</label> 
	        <label>mexican <input type="checkbox" name="food[]" value="mexican" /> 
	</label> 
	        <label>chinese <input type="checkbox" name="food[]" value="chinese" 
	        checked="checked" /></label> 
	    </fieldset> 
	    <input type="submit" value="go!" >&#39;); 
	} 
	 
	</body> 
	</html> 
登入後複製

多选checkbox:

<?php 
	$options = array(&#39;option 1&#39;, &#39;option 2&#39;, &#39;option 3&#39;); 
	 
	$valid = true; 
	if (is_array($_get[&#39;input&#39;])) { 
	    $valid = true; 
	    foreach($_get[&#39;input&#39;] as $input) { 
	        if (!in_array($input, $options)) { 
	            $valid = false; 
	        } 
	    } 
	    if ($valid) { 
	        //process input 
	    } 
	} 
	 
登入後複製

实例checkbox多值获取:

 class="brush:php;"><html> 
	<head> 
	    <title>using default checkbox values</title> 
	</head> 
	<body> 
	<?php 
	$food = $_get["food"];//www.phprm.com 
	if (!emptyempty($food)){ 
	    echo "the foods selected are: <strong>"; 
	    foreach($food as $foodstuff){ 
	        echo &#39;<br />&#39;.htmlentities($foodstuff); 
	    } 
	    echo "</strong>."; 
	} 
	else { 
	    echo (&#39; 
	    <form action="&#39;. htmlentities($_server["php_self"]).&#39;" method="get"> 
	        <fieldset> 
	            <label> 
	                italian 
	                <input type="checkbox" name="food[]" value="italian" /> 
	            </label> 
	            <label> 
	                mexican 
	                <input type="checkbox" name="food[]" value="mexican" /> 
	            </label> 
	            <label> 
	                chinese 
	                <input type="checkbox" name="food[]" value="chinese" checked="checked" /> 
	            </label> 
	        </fieldset> 
	        <input type="submit" value="go!" /> 
	    </form> &#39;); 
	    } 
	 
	</body> 
	</html> 
登入後複製

本文地址:

转载随意,但请附上文章地址:-)

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板