获取checkbox值的php 代码

 更新时间:2016年11月25日 15:51  点击:1763

由于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>
<?
print <<<here
chkfries: $chkfries <br>
chksoda: $chksoda <br>
chkshake: $chkshake <br>
chkketchup: $chkketchup <br>
<hr>

here;

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

?>
</body>
</html>


-->

实例

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[food];
$self = htmlentities($_server['php_self']);
if (!empty($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.3ppt.com
if (!empty($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>

php教程 soap 调用获取返回信息

trace soap message
获取 soap返回信息

<?php
   $ws = "http://www.111cn.net/sd/2001/temperatureservice.wsdl";
   $zipcode = "23590";
   $client = new soapclient($ws,array('trace' => 1));
   $temperature = $client->gettemp($zipcode);
   echo htmlspecialchars($client->__getlastrequest());
?>

得到函数原型从wsdl

 

<?php
   $ws = "http://www.www.111cn.net/sd/2001/temperatureservice.wsdl";
   $client = new soapclient($ws);
   var_dump($client->__getfunctions());
?>
 

<?php
   $ws = "http://www.xmethods.net/sd/2001/temperatureservice.wsdl";
   $zipcode = "12312";
   $client = new soapclient($ws,array('trace' => 1));
   $temperature = $client->gettemp($zipcode);
   echo htmlspecialchars($client->__getlastresponse());
?>
 

file_get_contents() 函数把整个文件读入一个字符串中。

和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。

语法
file_get_contents(path,include_path,context,start,max_length)参数 描述
path 必需。规定要读取的文件。
include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。
context 可选。规定文件句柄的环境。

context 是一套可以修改流的行为的选项。若使用 null,则忽略。
 
start 可选。规定在文件中开始读取的位置。该参数是 php教程 5.1 新加的。
max_length 可选。规定读取的字节数。该参数是 php 5.1 新加的。


php代码

<?php 
function post($url, $post = null) 

    $context = array(); 
 
    if (is_array($post)) 
    { 
        ksort($post); 
 
        $context['http'] = array 
        ( 
            'method' => 'post', 
            'content' => http_build_query($post, '', '&'), 
        ); 
    } 
 
    return file_get_contents($url, false, stream_context_create($context)); 

 
$data = array 

    'name' => 'test', 
    'email' => 'test@gmail.com', 
    'submit' => 'submit', 
); 
 
echo post('http://localhost/5-5/request_post_result.php', $data); 
?>  
接收数据:

request_post_result.php  接收经过post的数据:
php代码
<?php 
echo $_post['name']; 
echo $_post['email']; 
echo $_post['submit']; 
echo "fdfd"; 
?>  


实例二

/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://facebook.cn/restserver.php',$post_string);
*/
function request_by_other($remote_server,$post_string){
    $context = array(
        'http'=>array(
            'method'=>'post',
            'header'=>'content-type: application/x-www-form-urlencoded'."rn".
                      'user-agent : jimmy's post example beta'."rn".
                      'content-length: '.strlen($post_string)+8,
            'content'=>'mypost='.$post_string)
        );
    $stream_context = stream_context_create($context);
    $data = file_get_contents($remote_server,false,$stream_context);
    return $data;
}

数组的排序
在php教程中,排序方式有三种,通过索引排序、通过值排序(不保留原索引)、通过值排序(保留原索引)。每种又分为升序、降序以及用户定义顺序三个函数。它们分别如下:
通过索引排序:①升序 ksort() ②降序 krsort() ③用户定义顺序 uksort()
不保留原索引值排序:①升序 sort() ②降序 rsort() ③用户定义顺序 usort()
保留原索引值排序:①升序 asort() ②降序 arsort() ③用户定义顺序 uasort()
在php中,也可以用array_multisort来一次排序多个数组,不过项目中可能用得比较少。
翻转数组,把数字索引翻转,索引重新从0开始:array_reverse()
把索引和值调换:array_flip()
随机顺序:shuffle()
迭代器遍历

 

$test01 = array('a', 'b', 'c');
// for
for ($i = 0; $i < count($test01); $i++) {
    echo $test01[$i];
}
// foreach value only
foreach ($test01 as $value) {
    echo $value;
}
// foreach key and value
$test01 = array('a' => 'aaaa', 'b' => 'bbbb', 'c' => 'cccc');
foreach ($test01 as $key => $value) {
    echo "$key => $value";
}


$test01 = array('a' => 'aaaa', 'b' => 'bbbb', 'c' => 'cccc');
while (list($key, $value) = each($test01)) {
    echo "$key => $value" . "<br/>";
}

php中,迭代遍历主要要用到以下函数。
current() 迭代的当前元素。
reset() 重新移动到第一个元素并返回它。
next() 移动到下一个元素并返回它。
prev() 移动到上一个元素并返回它。
end() 移动到最后一个元素并返回它。
each() 以数组的形式返回当前元素的索引和值,并移动到下一个迭代。
key() 返回当前的索引。
array_ walk() 为每一个元素调用函数。
array_ reduce() 为每一个元素依次计算。

变通遍历
 

array_walk($test01, walk_test);
function walk_test($key, $value) {
    echo "walk: $key => $value" . "<br/>";
}
 
$test02 = array(1, 2, 3, 4, 5);
echo array_reduce($test02, reduce_test);
function reduce_test($run_result, $current_value) {
    return $run_result + $current_value * $current_value;
}

 

正则表达式的匹配规则修改如下:
([.n]*),当然,如果是在java程序中直接写到话,需要改为([.n]*)
结果再次运行程序,发现什么内容也取不到了。我百思不得其解,又将其修改为如下规则:
([.|n]*) 以及 ([n.]*)
结果还是不行,什么内容都取不到。看来点符号和换行符卯上劲了~
然后上网一查,虽然没有查出上述规则到底是什么地方出问题了,但是查出了一个解决办法,经过一试,果然可以匹配包括换行符在内的任意字符,以下为正确的正则表达式匹配规则:
([ss]*)
同时,也可以用 “([dd]*)”、“([ww]*)” 来表示。


在文本文件里, 这个表达式可以匹配所有的英文
/[ -~]/
这个表达式可以匹配所有的非英文(比如中文)
/[^ -~]/
/是vi里用的. 你在editplus或程序里不需要/

 

<form action="pcre.php教程" name="form1" method="post" >
输入将要匹配的字符串:<input type="text" name="str"  id="str" /><br />
<input type="submit" name="submit1" id="submit1" value="提交" />
</form>
<?php
$pattern="/d/";
if(preg_match($pattern, $str,$output)){
echo "匹配字符成功".$output;
}
else echo "匹配字符失败";
?>
</body>
[!--infotagslink--]

相关文章

  • PHP成员变量获取对比(类成员变量)

    下面本文章来给大家介绍在php中成员变量的一些对比了,文章举了四个例子在这例子中分别对不同成员变量进行测试与获取操作,下面一起来看看。 有如下4个代码示例,你认...2016-11-25
  • php 获取用户IP与IE信息程序

    php 获取用户IP与IE信息程序 function onlineip() { global $_SERVER; if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('HTTP_CLIENT_IP');...2016-11-25
  • php获取一个文件夹的mtime的程序

    php获取一个文件夹的mtime的程序了,这个就是时间问题了,对于这个问题我们来看小编整理的几个例子,具体的操作例子如下所示。 php很容易获取到一个文件夹的mtime,可以...2016-11-25
  • 如何获取网站icon有哪些可行的方法

    获取网站icon,常用最简单的方法就是通过website/favicon.ico来获取,不过由于很多网站都是在页面里面设置favicon,所以此方法很多情况都不可用。 更好的办法是通过google提供的服务来实现:http://www.google.com/s2/favi...2014-06-07
  • jquery如何获取元素的滚动条高度等实现代码

    主要功能:获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 :$(window).width(); 获取页面的文档高度 $(document).height(); 获取页面的文档宽度 :$(document).width();...2015-10-21
  • jquery获取div距离窗口和父级dv的距离示例

    jquery中jquery.offset().top / left用于获取div距离窗口的距离,jquery.position().top / left 用于获取距离父级div的距离(必须是绝对定位的div)。 (1)先介绍jquery.offset().top / left css: 复制代码 代码如下: *{ mar...2013-10-13
  • Ant design vue table 单击行选中 勾选checkbox教程

    这篇文章主要介绍了Ant design vue table 单击行选中 勾选checkbox教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-25
  • Jquery 获取指定标签的对象及属性的设置与移除

    1、先讲讲JQuery的概念,JQuery首先是由一个 America 的叫什么 John Resig的人创建的,后来又很多的JS高手也加入了这个团队。其实 JQuery是一个JavaScript的类库,这个类库集合了很多功能方法,利用类库你可以用简单的一些代...2014-05-31
  • C#获取字符串后几位数的方法

    这篇文章主要介绍了C#获取字符串后几位数的方法,实例分析了C#操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • DOM XPATH获取img src值的query

    复制代码 代码如下:$nodes = @$xpath->query("//*[@id='main_pr']/img/@src");$prurl = $nodes->item(0)->nodeValue;...2013-10-04
  • jquery获取tagName再进行判断

    如果是为了取到tagName后再进行判断,那直接用下面的代码会更方便: $(element).is('input') 如果是要取到标签用作到别的地方,可以使用一下代码: $(element)[0].tagName 或: $(element).get(0).tagName...2014-05-31
  • PHP 如何获取二维数组中某个key的集合

    本文为代码分享,也是在工作中看到一些“大牛”的代码,做做分享。 具体是这样的,如下一个二维数组,是从库中读取出来的。 代码清单: 复制代码 代码如下: $user = array( 0 => array( 'id' => 1, 'name' => '张三', 'ema...2014-06-07
  • php获取汉字拼音首字母的方法

    现实中我们经常看到这样的说明,排名不分先后,按姓名首字母进行排序。这是中国人大多数使用的排序方法。那么在php程序中该如何操作呢?下面就分享一下在php程序中获取汉字拼音的首字母的方法,在网上搜到的大多数是有问题的...2015-10-23
  • 使用C#获取系统特殊文件夹路径的解决方法

    本篇文章是对使用C#获取系统特殊文件夹路径的解决方法进行了详细的分析介绍,需要的朋友参考下...2020-06-25
  • php如何获取文件的扩展名

    网上也有很多类似的方法,不过都存在这样那样的不严谨的问题,本文就不一一分析了,这里只给出最正确的利用php 获取文件扩展名(文件后缀名)的方法。 function get_extension($filename){ return pathinfo($filename,PATHIN...2015-10-30
  • 基于JavaScript获取鼠标位置的各种方法

    这篇文章主要介绍了基于JavaScript获取鼠标位置的各种方法 ,需要的朋友可以参考下...2015-12-18
  • C#获取变更过的DataTable记录的实现方法

    这篇文章主要介绍了C#获取变更过的DataTable记录的实现方法,对初学者很有学习借鉴价值,需要的朋友可以参考下...2020-06-25
  • C#遍历得到checkboxlist选中值和设置选中项的代码

    这篇文章主要介绍了C#遍历得到checkboxlist选中值和设置选中项的代码,代码简单易懂,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 如何获取百度搜索结果页中解密之后的真实链接

    大家用百度搜索的时候,可能会发现,结果链接用的还是百度的链接,点击之后才会跳转到另外一个页面,另外一个页面的真实链接如何获取到呢?? 通过分析发现: 可以看出,返回...2016-05-19
  • C#设置与获取环境变量的方法详解

    这篇文章主要给大家介绍了关于C#设置与获取环境变量的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。...2020-06-25