Table of Contents
PHP programming must learn form verification every day, PHP programming form
Articles you may be interested in:
Home Backend Development PHP Tutorial PHP programming must learn form validation every day, PHP programming form_PHP tutorial

PHP programming must learn form validation every day, PHP programming form_PHP tutorial

Jul 12, 2016 am 08:58 AM
php form

PHP programming must learn form verification every day, PHP programming form

This article explains the implementation method of PHP form verification through examples, and shares it with everyone for your reference. The specific content is as follows

1.PHP form processing
welcome.html

<html>
<body>
  <form action="welcome.php" method="post">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="submit">
  </form>
</body>
</html>
Copy after login

welcome.php

<html>
<body>
<!--PHP 超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)-->

Welcome <&#63;php echo $_POST["name"]; &#63;><br>
Your email address is: <&#63;php echo $_POST["email"]; &#63;>

</body>
</html>
Copy after login

2. Complete example of PHP form verification

<html>
<head>
</head>
<body> 

<&#63;php
// 定义变量并设置为空值
$nameErr = $emailErr = $websiteErr = "";
$name = $email = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
   $nameErr = "姓名是必填的";
  } else {
   $name = test_input($_POST["name"]);
   // 检查姓名是否包含字母和空白字符
   //preg_match() 函数检索字符串的模式,如果模式存在则返回 true,否则返回 false。
   if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
    $nameErr = "只允许字母和空格"; 
   }
  }
  
  if (empty($_POST["email"])) {
   $emailErr = "电邮是必填的";
  } else {
   $email = test_input($_POST["email"]);
   // 检查电子邮件地址语法是否有效
   if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
    $emailErr = "无效的 email 格式"; 
   }
  }
   
  if (empty($_POST["website"])) {
   $website = "";
  } else {
   $website = test_input($_POST["website"]);
   // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠)
   if (!preg_match("/\b(&#63;:(&#63;:https&#63;|ftp):\/\/|www\.)[-a-z0-9+&@#\/%&#63;=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
    $websiteErr = "无效的 URL"; 
   }
  }

}

function test_input($data) {
  //去除用户输入数据中不必要的字符(多余的空格、制表符、换行)
  $data = trim($data);
  //删除用户输入数据中的反斜杠(\)
  $data = stripslashes($data);
  //把特殊字符转换为 HTML 实体
  $data = htmlspecialchars($data);
  return $data;
}
&#63;>

<h2>PHP 验证实例</h2>
<p><span class="error">* 必需的字段</span></p>
<!--htmlspecialchars() 函数把特殊字符转换为 HTML 实体。-->
<form method="post" action="<&#63;php echo htmlspecialchars($_SERVER["PHP_SELF"]);&#63;>"> 
  <!--value:保留表单中的值-->
  姓名:<input type="text" name="name" value="<&#63;php echo $name;&#63;>">
  <span class="error">* <&#63;php echo $nameErr;&#63;></span>
  <br><br>
  邮箱:<input type="text" name="email" value="<&#63;php echo $email;&#63;>">
  <span class="error">* <&#63;php echo $emailErr;&#63;></span>
  <br><br>
  网址:<input type="text" name="website" value="<&#63;php echo $website;&#63;>">
  <span class="error"><&#63;php echo $websiteErr;&#63;></span>
  <br><br>
  <input type="submit" name="submit" value="提交"> 
</form>

<&#63;php
echo "<h2>您的输入:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
&#63;>

</body>
</html>
Copy after login

I hope this article will help you learn PHP programming.

Articles you may be interested in:

  • Regular expressions commonly used for form validation in php
  • php form validation implementation code
  • Commonly used in PHP development Summary of the use of the three form validation functions
  • How to use the three PHP form validation functions ISSET(), empty(), and is_numeric()
  • php tp validation form and autofill function code
  • Detailed explanation of form verification based on PHP Ajax
  • Specific examples of using js for form verification on the PHP user registration page
  • ThinkPHP form automatic submission verification example tutorial
  • ThinkPHP Example of automatic form verification
  • thinkPHP implements automatic form verification

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1105381.htmlTechArticlephp programming must learn form verification every day, php programming form This article explains the implementation method of php form verification with examples, share For your reference, the specific content is as follows 1. PHP form processing w...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles