【问题标题】:PHP, display message if one or more fields is emptyPHP,如果一个或多个字段为空,则显示消息
【发布时间】:2014-03-02 10:18:12
【问题描述】:

我想要的是显示错误(消息),仅当用户执行错误操作时。例如,如果该字段为空,它将显示(请填写所有字段)。我已经这样做了,但我遇到的问题是,如果用户第一次进入页面,它也会显示,这意味着它不尊重我写的(如果条件)!

问题:

仅当其中一个字段为空时如何显示消息?

关于如何解决它的任何想法?

这是我的代码:

   <?
   $conn = mysqli_connect('localhost', 'db', 'db_pass', 'db_name') or die("Error " . mysqli_error($conn)); 
   $email = filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL);
   $old_password = trim($_POST['old_pass']);
   $new_password = trim($_POST['new_pass']);
   $email = mysqli_real_escape_string($conn,$email);
   $old_password = mysqli_real_escape_string($conn,$old_password);
   $new_password = mysqli_real_escape_string($conn,$new_password);
   if(empty($email) || empty($old_password) || empty($new_password)){
   echo 'Please fill all the fields !<br>';
   }
   else{
   $sql="UPDATE users SET pass='$new_password' WHERE email='$email' AND pass='$old_password'" or     die("Error " . mysqli_error($conn));
   $result = mysqli_query($conn,$sql);
   mysqli_close($conn);
   }
   if($result){
   echo'Password changed successfully !';
   }
   elseif(!$result) {
   echo 'The email/password you provided is false !';
   }
   ?>

【问题讨论】:

    标签: php


    【解决方案1】:

    任何形式的验证都发生在条件内的“操作”文件中,即验证应该受到用户单击提交按钮的事件的影响。为此,您应该检查一下

      1. Your form has a submit button with a name property set to say submit (can be anything)
    
    eg: <input type="submit" name="submit" id="someid" value="Submit" />
    
      2. The form must have action property pointing to a processor file
    
    eg: <form action = "somefile.php" method = "post">
    
      3. In the somefile.php file the validation code must be within a condition which checks for the event of form been submited
    
    eg://somefile.php
    
    <?php
      if(isset($_POST['submit']{
          //all the validation code goes here
      }else{
      //for a single page form and validation
      // the code for displaying the form can go here
    ?>
    

    【讨论】:

    • 您的代码似乎错过了将验证代码包装在条件中以检查表单是否已提交。希望这会有所帮助。
    【解决方案2】:

    我建议你这样做:

    • 首先用普通的 $_POST[] 定义一个变量,例如 $name = $_POST['name'];
    • 然后,检查您定义的所有变量是否为空。
    • 最后,使用 escape_string() 或任何你想要的。

    【讨论】:

      【解决方案3】:

      解决方案是检查一个您知道在提交表单时将始终设置的变量,通常是提交按钮。 例如,如果您的表单如下所示:

        ... 
        <input type="submit" name="change_password" value="Change password" />
      </form>
      

      然后在 PHP 代码中你可以检查

      if(isset($_POST['change_password'])) {
        // The submit button was in the POSTed data, so this is a form submit
      } else {
        // This is a new page load
      }
      

      或者,如果您正在POSTing 数据,您可以检查使用哪个 HTTP 方法调用表单:

      if($_SERVER['REQUEST_METHOD'] == 'POST') {
        // Form was posted
      } else {
        // $_SERVER['REQUEST_METHOD'] == 'GET'
      }
      

      我常用的模式是:

      $showForm = true;
      if( is_form_postback() ) { 
        if( data_is_valid() ) {
          redirect_to_thank_you_page();
        } else {
          show_validation_errors();
          $showForm = false;
        }
      }
      
      if($showForm) {
        // Print the form, making sure to set the value of each input to the $_POSTed value when available.
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-28
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-27
        相关资源
        最近更新 更多