网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 php校验表单检测字段是否为空的方法
内容
    这篇文章主要介绍了php校验表单检测字段是否为空的方法,涉及php验证表单的技巧,非常具有实用价值,需要的朋友可以参考下
    本文实例讲述了php校验表单检测字段是否为空的方法。分享给大家供大家参考。具体如下:
    php校验表单,检测字段是否为空,当表单中有未填写的字段,则会显示错误信息。
    <html>
    <body>
    <form METHOD="POST" ACTION="ErrorCheck.php">
    <h1>Contact Information</h1>
    <label>Nickname:</label>
    <input TYPE="TEXT" NAME="nickname">
    <label>Title:</label>
    <input TYPE="TEXT" NAME="title">
    <br />
    <input TYPE="SUBMIT" VALUE="Submit">
    <br />
    <input TYPE="RESET" VALUE="Clear the Form">
    </form>
    </body>
    </html>
    php后端代码,保存为: ErrorCheck.php
    <html>
    <body>
    <?php
    $errorcount=0;
    if (!trim($_POST['nickname'])) {
    echo "<br /><b>Nickname</b> is required.";
    $errorcount++;
    }
    if (!trim($_POST['title'])) {
    echo "<br /><b>Title</b> is required.";
    $errorcount++;
    }
    if ($errors > 0)
    echo "<br /><br />Please use your browser's back button " .
    "to return to the form, and correct error(s)";
    ?>
    </body>
    </html>
    trim()函数可以去除字符串中的前后空字符
    " " (ASCII 32 (0×20)), an ordinary space.
    "\t" (ASCII 9 (0×09)), a tab.
    "\n" (ASCII 10 (0x0A)), a new line (line feed).
    "\r" (ASCII 13 (0x0D)), a carriage return.
    "\0″ (ASCII 0 (0×00)), the NUL-byte.
    "\x0B" (ASCII 11 (0x0B)), a vertical tab.
    希望本文所述对大家的php程序设计有所帮助。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/13 4:56:08