Home > Backend Development > PHP Problem > How to prevent php user submission form from being empty?

How to prevent php user submission form from being empty?

coldplay.xixi
Release: 2023-03-02 11:28:01
Original
3307 people have browsed it

Solution to the problem that php user submission form cannot be empty: 1. Modify the code and add some judgments; 2. isset is used to judge whether the variable has been initialized; 2. empty can set the value to ["false", "Empty", "0", "NULL", "Uninitialized"] variables are all judged to be TRUE.

How to prevent php user submission form from being empty?

php user submission form cannot be empty solution:

You can modify the code and add some judgments:

The code is as follows:

  if(empty($_POST['name'])){
  echo "俗话说的好,雁过留声人过留名<br />";
 }
 elseif(empty($_POST[&#39;comment&#39;])){
  echo "矮油,多说几句吧~";
 }
 else{
  $sql = "INSERT INTO myblog_comments(blog_id, dateposted, name, comment) VALUES(" . $validentry . ", NOW(), &#39;" . $_POST[&#39;name&#39;] . "&#39;, &#39;" . $_POST[&#39;comment&#39;] . "&#39;);";
  mysql_query($sql);
  header("Location: http://". $_SERVER[&#39;HTTP_HOST&#39;] . $_SERVER[&#39;SCRIPT_NAME&#39;]."?id=" . $validentry);
 }
Copy after login

PHP null value judgment

empty and isset are both variables Processing functions, they are all used to determine whether the variable has been configured, but they have certain differences: empty will also detect whether the variable is empty or zero. When a variable value is 0, empty considers the variable to be equal to empty, which is equivalent to not being set.

The code is as follows:

<?php
/*比如检测 
$id 变量,当 $id=0 时,用empty 和 isset 来检测变量 
$id 是否已经配置,两都将返回不同的值—— empty 认为没有配置,isset 能够取得 
$id 的值:*/
    $id=0;
    empty($id)?print "It&#39;s empty .":print "It&#39;s $id .";
      //结果:It&#39;s empty .
    print "<br>";
    !isset($id)?print "It&#39;s empty .":print "It&#39;s $id .";
      //结果:It&#39;s 0 .
?>
Copy after login

Summary In PHP, "NULL" and "empty" are two concepts.

  • isset is mainly used to determine whether the variable has been initialized;

  • empty can set the value to "false" or "empty" , "0", "NULL", and "uninitialized" variables are all judged to be TRUE;

  • is_null Only variables with a value of "NULL" are judged to be TRUE;

  • var == null Judges variables with values ​​of "false", "empty", "0", and "NULL" as TRUE;

  • var === null Only variables with a value of "NULL" are judged as TRUE;

  • So when we judge whether a variable is really "NULL", we mostly use is_null to avoid " false", "0" and other values.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to prevent php user submission form from being empty?. For more information, please follow other related articles on the PHP Chinese website!

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