Home > Backend Development > PHP Tutorial > Detailed explanation of forms and validation examples in PHP introductory tutorial

Detailed explanation of forms and validation examples in PHP introductory tutorial

怪我咯
Release: 2023-03-10 19:16:01
Original
1185 people have browsed it

This article mainly introduces the form and verification skills of the PHP introductory tutorial, and analyzes the basic skills and related php formssubmission and data verification in the form of examples. Notes, need Friends can refer to

This article describes PHP forms and verification with examples. Share it with everyone for your reference, the details are as follows:

Demo1.php

<?php
  ob_start();
  //重新导向一个 URL
  //header()
  //header(&#39;Location:Demo2.php&#39;);
  //上面这句话可以自动跳转到你所想要的页面。
  //header(&#39;Location:http://www.baidu.com&#39;);
  //上面这句话自动跳转到百度上面去。
  echo &#39;baidu.com&#39;;
  header(&#39;Location:http://www.baidu.com&#39;);
  //在执行 header() 函数,必须注意,之前不能有任何浏览器输出
?>
Copy after login

Demo2.php

<?php
  ob_start();
  echo &#39;1232&#39;;
  //字符编码
  header(&#39;Content-Type:text/html;charset=GBK&#39;); //设置页面编码
  echo &#39;我是中文&#39;;
?>
Copy after login

Demo3.php

<form method="post" action="Demo4.php">
  姓名:<input type="text" name="username" /><br />
  <input type="submit" value="提交" />
</form>
Copy after login

Demo4.php

<?php
  //第一步,接收前面表单中的值。
  //一个,username
  //接收 $_POST[&#39;username&#39;]
  //echo $_POST[&#39;username&#39;];
  //你需要明白一个道理,空字符串也是数据,也可以赋值给 $_POST[&#39;username&#39;];
  //使用 isset() 验证是否正常提交是很准确的
  //目前所说的非法提交,是你没有经过表单提交,没有生成全局变量,而不是 username
  //这个字段为空
// if(isset($_POST[&#39;username&#39;])){
//   echo &#39;正常提交&#39;;
// }else{
//   echo &#39;非法提交&#39;;
// }
  //!empty($_POST[&#39;username&#39;]) 和 == &#39;&#39;基本一样,但是,并不能说,人家是非法的
  //只能说人家没有填而已。
// if(!empty($_POST[&#39;username&#39;])){
//   echo &#39;正常提交&#39;;
// }else{
//   echo &#39;非法提交&#39;;
// }
  //建议使用
  if(isset($_POST[&#39;username&#39;])){
    echo &#39;正常提交&#39;;
    //在输出之前,为了页面安全性
    //echo $_POST[&#39;username&#39;];
    $username = $_POST[&#39;username&#39;];
    $username = trim($username);
    $username = htmlspecialchars($username);
    if(strlen($username) < 2) {
      echo &#39;用户名不能小于两位&#39;;
      exit();
    }
    if(!is_numeric($username)) {
      echo &#39;用户名必须是纯数字&#39;;
      exit();
    }
    echo $username; //<strong>阅谁问君诵</strong>
  }else{
    echo &#39;非法提交&#39;;
  }
?>
Copy after login

Demo5.php

<form method="post" action="Demo6.php">
  用户名:<input type="text" name="username" /><br />
  密  码:<input type="text" name="password" /><br />
  验证码:<input type="text" name="code" size="5" />1234<br />
  邮  箱:<input type="text" name="email" /><br />
  介  绍:<textarea rows="6" cols="25" name="content"></textarea><br />
  <input type="submit" value="提交" name="send" />
</form>
Copy after login

Demo6.php

<?php
  //第一步,先验证是否是 Demo5.php 提交过来
  //只要是按钮点到我这里来的,那么就说明,其他超级变局变量都应该存在
  //如果 send 是存在的,那么就说是点过来,否则,跳回
  if(!isset($_POST[&#39;send&#39;]) || $_POST[&#39;send&#39;] != &#39;提交&#39;){
    header(&#39;Location:Demo5.php&#39;);
    exit; //跳回去了,下面就不需要执行了,那么就 exit;
  }
  //第二步,接收所有数据
  $username = trim($_POST[&#39;username&#39;]);
  $password = $_POST[&#39;password&#39;];
  $code = trim($_POST[&#39;code&#39;]);
  $email = trim($_POST[&#39;email&#39;]);
  $content = htmlspecialchars(trim($_POST[&#39;content&#39;]));
  //用户名不能小于 2 位,不能大于 10 位
  if(strlen($username)<2||strlen($username)>10){
    echo "<script>alert(&#39;用户名不能小于两位或者大于10&#39;);history.back();</script>";
    exit;
  }
  //密码不能小于六位
  if(strlen($password) <6){
    echo "<script>alert(&#39;密码不能小于6位&#39;);history.back();</script>";
    exit;
  }
  //验证码必须是 4 位,必须是数字
  if(strlen($code)!=4 || !is_numeric($code)){
    echo "<script>alert(&#39;验证码必须是 4 位并且是纯数字&#39;);history.back();</script>";
    exit;
  }
  //验证电子邮件
  if(!preg_match(&#39;/^([\w\.]{2,255})@([\w\-]{1,255}).([a-z]{2,4})$/&#39;,$email)){
    echo "<script>alert(&#39;电子邮箱不合法&#39;);history.back();</script>";
    exit;
  }
  echo &#39;用户名:&#39;.$username.&#39;<br/>&#39;;
  echo &#39;电子邮件:&#39;.$email.&#39;<br/>&#39;;
  echo &#39;个人介绍:&#39;.$content;
?>
Copy after login


The above is the detailed content of Detailed explanation of forms and validation examples in PHP introductory tutorial. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template