简体   繁体   English

PHP,如果一个或多个字段为空,则显示消息

[英]PHP, display message if one or more fields is empty

What I want is to show the error (message), only if the user do a false action. 我想要的是仅在用户执行错误操作时显示错误(消息)。 For example, if the field is empty, it will show (Please fill all the fields). 例如,如果该字段为空,则将显示(请填写所有字段)。 I've already done that, but the problem that I have is that it shows also if the user enter to the page for the first time, meaning it does NOT respects the (if condition) that I have written ! 我已经做完了,但是我遇到的问题是,如果用户第一次进入该页面,它也会显示出来,这意味着它不尊重我所写的(如果有条件的话)!

The question : 问题:

How to show the message only if one of the fields is empty ? 仅当其中一个字段为空时如何显示消息?

Any ideas on how I can solve it ? 关于如何解决的任何想法?

Here is my code : 这是我的代码:

  <? $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 !'; } ?> 

Validation of any form happens in the "action" file within a condition ie the validation should be subjected to the event of user clicking the submit button. 任何形式的验证都会在条件下的“操作”文件中发生,即,验证应受到用户单击“提交”按钮的事件的影响。 For this to work you should check that 为此,您应该检查一下

  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
?>

I suggest you to do this: 我建议你这样做:

  • First define a variable with plain $_POST[] for eg $name = $_POST['name']; 首先用简单的$ _POST []定义一个变量,例如$ name = $ _POST ['name'];
  • Then, check if all the vatiables you've define are empty or not. 然后,检查您定义的所有字样是否为空。
  • Lastly, Use escape_string() or whatever you want. 最后,使用escape_string()或任何您想要的东西。

The solution is to check for a variable that you know will always be set if the form is submitted, usually the submit button. 解决方案是检查是否知道在提交表单后将始终设置的变量,通常是“提交”按钮。 For example, if your form ends like this: 例如,如果您的表单以这种方式结束:

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

then in the PHP code you could check 然后在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
}

Alternatively, if you are POST ing the data, you can check which HTTP method was used to call the form: 或者,如果是POST荷兰国际集团的数据,则可以检查哪个HTTP方法用来调用形式:

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

The pattern I commonly use is: 我通常使用的模式是:

$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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM