获取checkbox值的php代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-04 14:07:12 浏览: 评论:0
由于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
- <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>
实例:
- <html>
- <head>
- <title>using default checkbox values</title>
- </head>
- <body>
- <?php
- $food = $_get[food];
- $self = htmlentities($_server['php_self']);
- 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 ('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!" >');
- }
- ?>
- </body>
- </html>
多选checkbox:
- <?php
- $options = array('option 1', 'option 2', 'option 3');
- $valid = true;
- if (is_array($_get['input'])) {
- $valid = true;
- foreach($_get['input'] as $input) {
- if (!in_array($input, $options)) {
- $valid = false;
- }
- }
- if ($valid) {
- //process input
- }
- }
- ?>
实例checkbox多值获取:
- <html>
- <head>
- <title>using default checkbox values</title>
- </head>
- <body>
- <?php
- $food = $_get["food"];//www.phpfensi.com
- if (!emptyempty($food)){
- echo "the foods selected are: <strong>";
- foreach($food as $foodstuff){
- echo '<br />'.htmlentities($foodstuff);
- }
- echo "</strong>.";
- }
- else {
- echo ('
- <form action="'. htmlentities($_server["php_self"]).'" 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> ');
- }
- ?>
- </body>
- </html>
Tags: checkbox值 php代码
- 上一篇:php soap 调用获取返回信息
- 下一篇:php curl post数据的问题
相关文章
- ·获取checkbox值的几种方法(2014-08-16)
- ·PHP代码优化及PHP相关问题总结(2013-11-27)
- ·一段防盗链的PHP代码(2013-12-08)
- ·PHP代码实现301跳转页面实例(2014-01-11)
- ·非常实用的PHP代码片段(2014-07-03)
- ·PHP代码中swf动画的调用(2014-07-23)
- ·PHP万年历实现程序代码(2014-07-30)
- ·php中Ubb代码编辑器程序代码(2014-07-31)
- ·用户注册检测用户名是否存在ajax+php代码(2014-08-15)
- ·PHP代码安全性问题的建议(2014-08-25)
- ·php编程代码规范学习笔记(适合初学者)(2015-04-08)
- ·值得收藏的10个PHP代码样例(黑名单\下载文件)(2015-04-08)
- ·判断用户是PC还是移动端的php代码(2015-12-24)
- ·非常有用的9个PHP代码片段(2019-10-04)
- ·9个比较实用的php代码片段(2019-11-18)
- ·PHP代码优化的53个细节(2020-10-12)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)