php页面 表单传递参数实例教程

 更新时间:2016年11月25日 15:10  点击:1694
在php教程中要传递参数有几种方法,今天我们就讲关于post get二种方法,post用得最多的是利用表单来传参数,get参数一般是url传值居多,下面我们举了二款实例。

post表单传递参数

 代码如下 复制代码
<html>
<head>
</head>
<body>
<h3>search</h3>
<form action="c.php" method="post">
depart name:<input type="text" size=25 name="depart" value=""><br><br>
<input type="submit" name="submit" value="search">
</form>
</body>
</html>

c.php页面

 

 代码如下 复制代码
<?php
$depart=$_post["depart"];
$q = "select * from info where depart='$depart'";
?>

实例二get 方法传递参数

 代码如下 复制代码
<a href=www.111cn.net/a.php?value=www.111cn.net>传get参数传</a>


a.php页面代码

 代码如下 复制代码
echo $_get['value'];

 

 

这款函数比较实用于数据采集,一般采集数据时会要指定一个区域的内容,这个代码就可以实例;
*/

 代码如下 复制代码

$str="<!-- fdaf-- -->";
echo strip_comments( $str );

function strip_comments($data) {
    $the_rest = $data;
    $result = "";

    while ($the_rest) {
        $start = strpos($the_rest, "<!--");
        if ($start === false) {
            $result .= $the_rest;
            break;
        }

        $result .= substr($the_rest, 0, $start);

        $end = strpos($the_rest, "-->", $start);
        if ($end === false) {
            break;
        }

        $the_rest = substr($the_rest, $end+3);
    }

    return $result;
}

 代码如下 复制代码

*/
function attachicon($type,$size='') {
 static $attachicons = array(
 0 => 'common.gif',
 1 => 'image.gif',
 2 => 'binary.gif',
 3 => 'rar.gif',
 4 => 'msoffice.gif',
 5 => 'text.gif',
 6 => 'html.gif',
 7 => 'real.gif',
 8 => 'av.gif',
 9 => 'flash.gif',
 10 => 'pdf.gif',
 11 => 'torrent.gif'
 );

 if(preg_match("/image|^(jpg|gif|png|bmp)/", $type)) {
  $typeid = 1;
 } elseif(preg_match("/bittorrent|^torrent/", $type)) {
  $typeid = 11;
 } elseif(preg_match("/pdf|^pdf/", $type)) {
  $typeid = 10;
 } elseif(preg_match("/flash|^(swf|fla|swi)/", $type)) {
  $typeid = 9;
 } elseif(preg_match("/audio|video|^(wav|mid|mp3|m3u|wma|asf|asx|vqf|mpg|mpeg|avi|wmv)/", $type)) {
  $typeid = 8;
 } elseif(preg_match("/real|^(rm|rv)/", $type)) {
  $typeid = 7;
 } elseif(preg_match("/htm|^(php|js|pl|cgi|asp教程)/", $type)) {
  $typeid = 6;
 } elseif(preg_match("/text|^(txt|rtf|wri|chm)/", $type)) {
  $typeid = 5;
 } elseif(preg_match("/word|powerpoint|^(doc|ppt)/", $type)) {
  $typeid = 4;
 } elseif(preg_match("/compressed|^(zip|arj|rar|arc|cab|lzh|lha|tar|gz)/", $type)) {
  $typeid = 3;
 } elseif(preg_match("/octet-stream|^(exe|com|bat|dll)/", $type)) {
  $typeid = 2;
 } else {
  $typeid = 0;
 }

 return $size.$attachicons[$typeid];
}

在php教程中要上传文件那简单的利用move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。

语法
move_uploaded_file(file,newloc)参数 描述
file 必需。规定要移动的文件。
newloc 必需。规定文件的新位置

 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php文件上传函数</title>
</head>

<body>

<form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="hidden" name="max_file_size" value="30000" />
    <input name="userfile" type="file" />
    <input type="submit" value="send file" />
</form>
</body>
</html>

 代码如下 复制代码
<?
if( $_post )
{
 if( uploadfile( "userfile" ) )
 {
  echo '文件上传成功';
 }
 else
 {
  echo '文件上传失败';
 }
}
//参数   $file   为   前台file控件的name;
function     uploadfile($file)
{
    $uploaddir   =   $_server[ 'document_root ']. '/www.111cn.net/uploadfile/ ';
    $file_name   =   $uploaddir.rand(1,1000000). ". ".fileextend($_files[$file][ 'name ']);
    if   (move_uploaded_file($_files[$file][ 'tmp_name '],$file_name))  
    {
    return   true;
    }
    else
    {
        return   false;
    }
}
?>

提示和注释
注释:本函数仅用于通过 http post 上传的文件。

注意:如果目标文件已经存在,将会被覆盖。

文章收藏了三款php教程遍历二维数组,在这里我们只讲利用了 foreach函数来遍历二维数组的方法,好了下面来看看吧。
 代码如下 复制代码
<?php
 $team = array('lk','ok');
 $book = array('linux服务器配置与管理',$team);
 
 foreach($book as $k=>$val)  //意思是for  $book  each  $value( as )
  if( is_array($val) ) foreach( $val as $value) echo $value.'<br />';
  else echo $k.'=>'.$val.'<br />';
?>


 
则输出为:
0=>linux服务器配置与管理
lk
ok


方法二

 代码如下 复制代码

$arr=array();
foreach ($row_mark as $arr)
{
print_r("<tr>");
foreach ($arr as $k=>$v)
{
print_r("<td width='80'>".$v."</td>");
}
print_r("</tr>");
}


第三种情况

$arr = array(
  array('name'=>'www.111cn.net备布置','url'=>'?action=config&do=config'),
  array('name'=>'验证码配备布置','url'=>'?action=config&do=seccode'),
  array('name'=>'模板管理','url'=>'www.111cn.net?action=config&do=tpl'),
  array('name'=>'帐号管理','url'=>'?action=admin&do=list'),
  array('name'=>'新增帐号','url'=>'?action=admin&do=add'));
  foreach($arr as $k=>$val){
  echo "name:".$val["name"]."n";
  }

[!--infotagslink--]

相关文章