Php 显示消息/错误-表单验证

Php 显示消息/错误-表单验证,php,jquery,html,forms,Php,Jquery,Html,Forms,由于我是web开发新手,并且我想朝着正确的方向前进,所以我有一个关于表单验证的问题。所以,我有一个小表单,用来向数据库中添加一些信息。显然,为此我需要使用PHP。提交表单后,我需要停留在同一页面上,但我需要在表单下方向用户发送消息/错误(如果用户将输入字段留空,或者数据库事务已成功完成,或者根本没有完成) 这是我的表格: <form method="post" action="index.php" name="addOperationForm"> <input type

由于我是web开发新手,并且我想朝着正确的方向前进,所以我有一个关于表单验证的问题。所以,我有一个小表单,用来向数据库中添加一些信息。显然,为此我需要使用PHP。提交表单后,我需要停留在同一页面上,但我需要在表单下方向用户发送消息/错误(如果用户将输入字段留空,或者数据库事务已成功完成,或者根本没有完成)

这是我的表格:

<form method="post" action="index.php" name="addOperationForm">
    <input type="text" class="input" placeholder="Name of operation" name="nameOfSearch"/>
    <input type="text" class="input" placeholder="Security code" name="securityCode"/>
    <input type="submit" class="btns" value="Add to dB" name="submitAddSearch"/>
</form>

到目前为止,我使用单独的PHP文件-index.PHP来建立与数据库的连接并向数据库添加数据


要管理向用户发送消息,我应该同时使用PHP和HTML代码(然后以某种方式在表单下回显消息),还是应该使用jQuery(但如何才能建立数据库连接)?最终,正确的做法是什么?您可以发布一个示例吗?

您可以使用jQuery进行表单验证(空检查)

表格

<form method="post" action="index.php" name="addOperationForm">
    <input type="text" class="input" placeholder="Name of operation" name="nameOfSearch"/>
    <input type="text" class="input" placeholder="Security code" name="securityCode"/>
    <input type="button" id="submit_btn" class="btns" value="Add to dB" name="submitAddSearch"/>
</form>

您可以做的是制作一个php脚本来处理验证并以json格式返回响应,以便jQuery可以使用ajax处理请求

例如在response.php中

<?php
  //Get submitted data from the form
  $name = $_POST["name"];

  //sample validation character is less than 2 char
  if(strlen($name) < 2){
    $response = array(
                "error" => true,
                "message" => "Name should be 2 characters or more"
           );
  }else{
      $response = array(
           "error" => false,
           "message" => "Name is valid"
      );
  }

//Return the response for ajax request
     echo json_encode($response);

代码未经测试,但应适用于您的用例。

这可能被归类为过于宽泛,因为每个人都有自己的方法以多种不同的方式向用户显示错误。您可以将所有错误存储在一个数组中,然后立即将它们吐出到页面上,也可以一次显示一个错误。使用jQuery将使其更具交互性,但这取决于您作为开发人员。在担心jQuery甚至JavaScript之前,我建议您先学习HTML和PHP技能,直到您熟悉为止。您能告诉我您的方法是什么,或者您推荐什么是正确的或好的方法吗?正确的或好的是每个人定义不同的术语,因此,我最初的评论是,这个问题可能被归类为过于宽泛。一个人的好可能是另一个人的坏。找到一种你熟悉的方法并使用它。为什么这是正确的验证方法?考虑解释你的代码,因为OP对于Web开发是新的。我会给你的代码一个尝试。当然,PAL,我只是告诉你怎么做它的想法:)我有一个简单的问题。当php脚本完成后,我是否使用header转到我的html页面?您可以通过php或jQuery进行操作,如果使用jQuery或javascript,则只需在成功验证时添加window.location.href=“url到html页面”如果使用php,则需要使用header。我将重定向添加到我的答案
str_len()
,它不是一个核心PHP函数,该函数是
strlen()
<?php
  //Get submitted data from the form
  $name = $_POST["name"];

  //sample validation character is less than 2 char
  if(strlen($name) < 2){
    $response = array(
                "error" => true,
                "message" => "Name should be 2 characters or more"
           );
  }else{
      $response = array(
           "error" => false,
           "message" => "Name is valid"
      );
  }

//Return the response for ajax request
     echo json_encode($response);
$(document).ready(function(){
     $.ajax({
         url : "response.php", //the php script that will handle the validation
         method : "post"
     }).done(function(response){
        //handle the response
         if(response.error===false){
             //output the response message below the form
             //success has div with class success
             $("<div class='success'>" + response.message + "</div>").appendTo("form[name='addOperationForm']");

             //redirect to a page
             //  window.location.href = "nextpage.php";
         }else{
            //error has class='error'
             $("<div class='error'>" + response.message + "</div>").appendTo("form[name='addOperationForm']");
         }
     });
});
.success { background-color : #0f0; color : #fff; } /* background is green for success */
.error {background-color : #f00; color : #fff;} /* background is red for error */