php trim() 表单验证不为空实例
php教程 trim() 表单验证不为空实例,应该算是入门级的实例了,告诉你如何利用trim函数来删除空格然后判断用户提交的数据是否为空。
<html>
<body>
<form method="post" action="formerrorcheck.php">
<h1>contact information</h1>
<table><tr>
<td><b>nickname:</b></td>
<td><input type="text" name="nickname"></td>
</tr><tr>
<td>title:</td>
<td><input type="text" name="title"></td>
</tr><tr>
<td><b>first name:</b></td>
<td><input type="text" name="firstname"></td>
</tr><tr>
<td>middle name:</td>
<td><input type="text" name="middlename"></td>
</tr><tr>
<td><b>last name:</b></td>
<td><input type="text" name="lastname"></td>
</tr><tr>
<td><b>primary email:</b></td>
<td><input type="text" name="email"></td>
<td width="20"> </td>
<td>secondary email:</td>
<td><input type="text" name="secondaryemail"></td>
</tr><tr>
<td>company name:</td>
<td><input type="text" name="companyname"></td>
</tr><tr>
<td>office address:</td>
<td><input type="text" name="officeaddres1"></td>
<td width="20"> </td>
<td>home address:</td>
<td><input type="text" name="homeaddress"></td>
</tr><tr>
<td></td>
<td><input type="text" name="officeaddress2"></td>
</tr><tr>
<td>city:</td>
<td><input type="text" name="officecity"></td>
<td width="20"> </td>
<td> </td>
<td><input type="text" name="homecity"></td>
</tr><tr>
<td>state:</td>
<td><input type="text" name="officestate"></td>
<td width="20"> </td>
<td> </td>
<td><input type="text" name="homestate"></td>
</tr><tr>
<td>zip:</td>
<td><input type="text" name="officezip"></td>
<td width="20"> </td>
<td> </td>
<td><input type="text" name="homezip"></td>
</tr><tr>
<td>phone:</td>
<td><input type="text" name="officephone"></td>
<td width="20"> </td>
<td> </td>
<td><input type="text" name="homephone"></td>
</tr><tr>
<td>birthday:</td>
<td><input type="text" name="birthday"></td>
</tr><tr>
<td>spouse name:</td>
<td><input type="text" name="spousename"></td>
<td width="20"> </td>
<td>childrens' names:</td>
<td><input type="text" name="children"></td>
</tr><tr>
<td>anniversary:</td>
<td><input type="text" name="anniversary"></td>
</tr></table>
<br>
<br>
<br>
<input type="submit" value="submit">
<br>
<br>
<input type="reset" value="clear the form"></form>
</body>
</html><!-- formerrorcheck.php
<html>
<body>
<?php$errors=0;
if (!trim($nickname)) {
echo "<br><b>nickname</b> is required.";
$errors++;
}
if (!trim($firstname)) {
echo "<br><b>first name</b> is required.";
$errors++;
}
if (!trim($lastname)) {
echo "<br><b>last name</b> is required.";
$errors++;
}
if (!trim($email)) {
echo "<br><b>primary email address</b> is required.";
$errors++;
}if ($errors > 0)
echo "<br><br><br>please use your browser's back button " .
"to return to the form, and correct error(s)";
?></body>
</html>
这是个简单的验证函数
<?php
function phone_validate($data, $desc) {
$regex = "/^([2-9][0-9]{2})[2-9][0-9]{2}-[0-9]{4}/i";
if(preg_match($regex, $data) != 1) {
return "the '$desc' field isn't valid!";
}
return true;
}
?>
上传文件我们少不了move_uploaded_file函数本函数检查并确保由 file 指定的文件是合法的上传文件(即通过 php教程 的 http post 上传机制所上传的)。如果文件合法,则将其移动为由 newloc 指定的文件。
如果 file 不是合法的上传文件,不会出现任何操作,move_uploaded_file() 将返回 false。
如果 file 是合法的上传文件,但出于某些原因无法移动,不会出现任何操作,move_uploaded_file() 将返回 false,此外还会发出一条警告。
先来看一个关于在上传关表单设置
<html>
<head>
<title>a simple file upload form</title>
</head>
<body>
<form enctype="multipart/form-data"
action="<?print $_server['php_self']?>" method="post">
<p>
<input type="hidden" name="max_file_size" value="102400" />
<input type="file" name="fupload" /><br/>
<input type="submit" value="upload!" />
</p>
</form>
</body>
</html>
这样我们主设置的上传文件最大不能超过102400字节了
在php中要实现文件上传很简单如下代码
if ( $_files['fupload']['type'] == "image/gif" ) {
$source = $_files['fupload']['tmp_name'];
$target = "upload/".$_files['fupload']['name'];
move_uploaded_file( $source, $target );// or die ("couldn't copy");
$size = getimagesize( $target );$imgstr = "<p><img width="$size[0]" height="$size[1]" ";
$imgstr .= "src="$target" alt="uploaded image" /></p>";print $imgstr;
}
这样就ko了,下面我们总结实例
php
<html>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="max_file_size" value="6000000" /> <!--设置允许提交表单的最大字节数-->
文件上传: <input name="file" type="file" />
<input type="submit" value="上传"/>
</form>
</html>
<?
//处理程序
function extend($file_name) //定义获取文件的扩展名函数
{$extend =explode("." , $file_name);
$va=count($extend)-1;
return strtolower($extend[$va]);}$dirname="../";//是否启用上一层路径,格式为:$dirname="../";或$dirname="http://www.cnblogs.com/";等等与$dir组合使用,注意不要溢出根路径
$dir='upload/aa/bb';//设定上传目录,与上面的$dirname组合
$file=$_files['file'];//从文件域表单获取文件
$filename=$file['name'] ;//获取文件全名
$c_filesize=$file['size'] ;//获取本地的文件大小
$extendname=extend($filename);//获取文件扩展名
if($c_filesize>200000000000)die("文件太大");//限制上传文件大小, 单位字节//if($extendname!="jpg")die("只允许上传jpg格式的图片");//限制上传文件格式,去掉语句开头的两斜杠生效
if(!file_exists($dir));//检查目录文件夹是否存在,不存在则建立新文件夹
{
$v=split ('[/.-]', $dir);
for ( $i=0 ; $i <count($v) ; $i++)
{$dirname=$dirname.$v[$i];
if(!file_exists($dirname))mkdir($dirname);
$dirname=$dirname."/";}
} //目录创建完毕
?><?
$dest=$dirname.date("ymdhis", time()).rand(100000,999999).".".$extendname; //设置文件名为日期加上从100000到999999的随机数和扩展名
if(file_exists($dest))die("该文件已经存在");
if(move_uploaded_file($file['tmp_name'],$dest)) //调用文件上传函数
{$s_filesize=filesize($dest);//获取服务器端的文件大小
echo "文件上传成功,<a href=".$dest.">查看文件地址</a>";
echo "<br>本地文件名:".$filename;
echo "<br>远端文件名:".$dest;
echo "<br>大 小:".ceil($s_filesize/1024)." kb";
echo "<br>扩展名:".$extendname;
echo "<br>大 小:".$c_filesize." byte";}
else
{echo "还未进行文件上传";}
?>
总结了文件上传的错误代码
upload_err_ok没有错误。
upload_err_ini_size的上传的文件超过最高价值存在中指定的文件。
upload_err_form_size的上传的文件超过最高价值所指定的max_file_size隐藏的部件。
upload_err_partial的文件上传被取消了,只有部分的文件被上传。
upload_err_nofile没有文件被上传。
检测数据类型php教程函数集
检测数据类型即对数据类型进行检测,判断所检测类型是否属于检测类型,符合则返回真,否则返回假。检测数据类型定义如下:
is_bool
是否为布尔类型,例,is_bool(srue) is_bool(false)
is_string
是否为字符串型,例,is_string(‘string’) is_string(1234)
is_float/double
是否为浮点型,例,is_float(3.1415) is_float(‘3.1415’)
is_integer/int
是否为整型,例,is_integer(34) is_integer(‘34’)
is_null
是否为空值,例,is_null(null)
is_array
是否为数组,例,is_array($arr)
is_object
是否为一个对象,例,is_object($obj)
is_numeric
是否为数字或由数字组成的字符串,例,is_numeric(‘5’) is_numeric(‘bcc110’)
示例
<?php
$boo="1234567890";
if(is_numeric($boo))
echo "变量boo属由数字组成的字符串类型:".$boo;
else
echo"无法判断";
?>
php教程 strtotime()计算今天与指定日期之天数
$date1 = strtotime('2011-04-30'); //把日期转换成时间戳
$date2 = time(); //取当前时间的时间戳
$nowtime=strftime("%y年-%m月-%d日 ",$date2); //格式化输出日期
$days=round(($date1-$date2)/3600/24); //四舍五入
echo "今天是<font color="red">".$nowtime."</font>";
echo "<br/>距".strftime("%y年-%m月-%d日 ",$date1)."还有<font colr="red">".$days."</font>天";
echo date("y-m-d h:i:s",strtotime("now")). "<br />";
echo date("y-m-d h:i:s",strtotime("10 september 2000")). "<br />";
echo date("y-m-d h:i:s",strtotime("+2 day")). "<br />";
echo date("y-m-d h:i:s",strtotime("+1 week")). "<br />";
echo date("y-m-d h:i:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br />";
echo date("y-m-d h:i:s",strtotime("next thursday")). "<br />";
echo date("y-m-d h:i:s",strtotime("last monday")). "<br />";
echo date("y-m-d h:i:s",strtotime("+3 day",strtotime('2001-01-01')));
php教程中checkbox值获取,显示,多选值获取
最简单checkbox获取值代码
<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"];
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>
相关文章
- artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口。本文给大家介绍JS中artdialog弹出框控件之提交表单思路详解,对本文感兴趣的朋友一起学习吧...2016-04-19
- 有时候我们为了省事就操作键盘组合键去代替使用鼠标,我们今天就使用JQuery实现Ctrl+Enter提交表单。我们发帖时,在内容输入框中输入完内容后,可以点击“提交”按钮来发表内容。可是,如果你够“懒”,你可以不用动鼠标,只需按...2015-10-23
Django def clean()函数对表单中的数据进行验证操作
这篇文章主要介绍了Django def clean()函数对表单中的数据进行验证操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-09- 为了网站的安全性,很多朋友都把密码设的比较复杂,但是如何密码不能明显示,不知道输的是对是错,为了安全起见可以把密码显示的,那么基于js代码如何实现的呢?下面通过本文给大家介绍JavaScript实现表单密码的隐藏和显示,需要的朋友参考下...2016-03-03
- 这篇文章主要为大家详细介绍了JavaScript实现密码框输入验证,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-01
- 这篇文章主要为大家详细解析了BootStrap栅格系统、表单样式与按钮样式源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-23
- 这篇文章主要介绍了Nest.js 授权验证的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-22
- 这篇文章主要介绍了el-table树形表格表单验证(列表生成序号),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-01
- 这篇文章主要介绍了selenium 反爬虫之跳过淘宝滑块验证功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-27
vue element table中自定义一些input的验证操作
这篇文章主要介绍了vue element table中自定义一些input的验证操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-18- 这篇文章主要为大家详细介绍了js canvas实现滑块验证,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
- 这篇文章主要介绍了react使用antd表单赋值,用于修改弹框的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29
- 这篇文章主要为大家详细介绍了vue实现表单验证小功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-29
- 在html表单提交中method请求Get和Post区别其实很显示的,get提交会是url形式的并且数据量不能太多,而post数据是在浏览器url看不到的并且可以是大数据量而且get安全性非...2016-09-20
- 这篇文章主要介绍了微信小程序 PHP后端form表单提交实例详解的相关资料,需要的朋友可以参考下...2017-01-16
- 这篇文章主要介绍了解决antd Form 表单校验方法无响应的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-28
基于Bootstrap实现Material Design风格表单插件 附源码下载
Jquery Material Form Plugin是一款基于Bootstrap的Material Design风格的jQuery表单插件。这篇文章主要介绍了基于Bootstrap的Material Design风格表单插件附源码下载,感兴趣的朋友参考下...2016-04-19- 本篇文章主要介绍了vue2 中如何实现动态表单增删改查实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...2017-06-15
- 这篇文章主要为大家详细介绍了JavaScript表单验证示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-04
- 这篇文章主要给大家介绍了关于利用Python验证的50个常见正则表达式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-11