Home > Backend Development > PHP Tutorial > Backend - How to intercept the content in the form using PHP string?

Backend - How to intercept the content in the form using PHP string?

WBOY
Release: 2016-08-27 09:06:51
Original
1068 people have browsed it

HTML code, form:

<code>        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <input type="text" name="name" class="a1-input" placeholder="名字">
            <div class="a1-btn-min"><button class="a1-btn waves-effect waves-light">YES</button></div>
        </form></code>
Copy after login
Copy after login

PHP code, verification:

<code><?php

$name = "";
$name_str = mb_strlen($name,"UTF8");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["name"])) {
            echo "<script>alert('你并没有写你的名字...')</script>";
      } else if($name_str >= 10){
            echo "<script>alert('这貌似不是一个名字吧~!')</script>";
      } else{
            $name = test_input($_POST["name"]);
      }
}
  
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

?></code>
Copy after login
Copy after login

For the html and php code above, an empty variable named name is set, but later the character length filled in by the user in the form has to be obtained and verified.
However, when verifying the length, the length of the information filled in by the user cannot be verified. How to solve this problem?

Reply content:

HTML code, form:

<code>        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <input type="text" name="name" class="a1-input" placeholder="名字">
            <div class="a1-btn-min"><button class="a1-btn waves-effect waves-light">YES</button></div>
        </form></code>
Copy after login
Copy after login

PHP code, verification:

<code><?php

$name = "";
$name_str = mb_strlen($name,"UTF8");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["name"])) {
            echo "<script>alert('你并没有写你的名字...')</script>";
      } else if($name_str >= 10){
            echo "<script>alert('这貌似不是一个名字吧~!')</script>";
      } else{
            $name = test_input($_POST["name"]);
      }
}
  
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

?></code>
Copy after login
Copy after login

For the html and php code above, an empty variable named name is set, but later the character length filled in by the user in the form has to be obtained and verified.
However, when verifying the length, the length of the information filled in by the user cannot be verified. How to solve this problem?

You have to assign the $_POST["name"] you got to $name, or directly mb_strlen($_POST["name"],"UTF8")

Because you did not proceed in the if judgment

<code>$name_str = mb_strlen($name,"UTF8");</code>
Copy after login

$name = $_POST["name"]; That's it

Related labels:
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