如何在使用ajax和php提交表单后显示警告提醒?

时间:2017-11-30 08:22:59

标签: php ajax

我在用户管理部分有一个场景,在创建用户之前,我需要确保用户名已经存在。成功插入成功消息显示在表单提交后,但如果我尝试使用现有用户名创建新用户,我无法弄清楚如何显示警告提示。为了检查用户名是否已经存在,我已经检查了用户表中的行,我尝试的代码如下所示......

$sql="select * from user where username='$username'";
$result=mysqli_query($connection, $sql);
$row_no=mysqli_num_rows($result);
/*if($row_no<1){*/
if($_POST["user_id"] != '')  
  {  
       $query = "  
       UPDATE user   
       SET fullname='$fullname',   
       user_role='$user_role',   
       username='$username',   
       password = '$password',   
       branch_id = '$branch'   
       WHERE user_id='".$_POST["user_id"]."'";  
       $message = 'Data Updated'; 
       if($_SESSION['user_id']==$_POST["user_id"]){
       $_SESSION['username']=$username; 
       $_SESSION['user_role']=$user_role;
       $_SESSION['fullname']=$fullname; 

       }

  } else{

$query = "
INSERT INTO user(fullname, user_role, username, password, branch_id,registered_date)  
 VALUES('$fullname', '$user_role', '$username', '$password', '$branch','$registered_date')


";

  }
if(mysqli_query($connection, $query))
{

 $select_query = "SELECT * FROM user ORDER BY user_id DESC";
 $result = mysqli_query($connection, $select_query);
 $output .= '

                 <table style="background-color:#FFF;" id="employee_data" class="table table-striped table-bordered">  
                      <thead>  
                           <tr>  
                                <th>User ID</th>  
                                <th>Fullname</th>  
                                <th>User Role</th>  
                                <th>Username</th>
                                <th>Password</th>  
                                <th>Branch</th> 
                                 <th>Registered Date</th>
                                 <th>Action</th>  


                           </tr>  
                      </thead> 

 ';
while($row = mysqli_fetch_array($result))
 {

  $output .='

   <tr>  
                                <td>'.$row["user_id"].'</td>  
                                <td>'.$row["fullname"].' </td>
                                <td>'.$row["user_role"].'</td>
                                <td>'.$row["username"].'</td>
                                 <td>'.$row["password"].'</td> 
                                 <td>'.$row["branch_id"].'</td>
                                <td>'.$row["registered_date"].'</td>  
                         <td>

 <Button id="'.$row["user_id"].'" class="btn btn-warning btn-xs edit_data">
 <i class="fa fa-edit "></i></Button> &nbsp' ;
if($_SESSION['user_id']!=$row["user_id"]) {
 $output .='<a id="'.$row["user_id"].'"  class="btn btn-danger btn-xs 
 delete"><i class="fa fa-times"></i></a>';
 }



 $output .= '</td></tr>';
 }
 $output .= '
 </table>

 <div id="success-alert" class="alert alert-success fade in m-b-15">
                            <strong>Success!</strong>
                            Data Updated.
  <span class="close" data-
  dismiss="alert">×</span>
                        </div>


<script>$("#success-alert").fadeTo(2000, 
500).slideUp(500, function(){
$("#success-alert").slideUp(600); });</script>

                        ';
}


    echo $output;


}

1 个答案:

答案 0 :(得分:0)

要阻止提交,您可以使用...

$(document).ready(function(e){
    e.preventDefault();
    // Rest of your code...
});

并显示错误消息......

您可以在处理程序中创建一个数组,并在数组中添加错误 在所有验证之后,在阵列上再进行一次验证,并填写错误 如果有错误,您可以显示div并用数组填充它们。