我的Ajax表单提交不起作用

My Ajax form submit is not working?

本文关键字:不起作用 表单提交 Ajax 我的      更新时间:2023-09-26

我的表单无法工作。即使查询没有像我希望用户名是唯一的那样执行,它也会成功,所以我想在用户名不唯一时触发一个错误,如果成功,我想重定向到一个页面,即"clients.php",所以这是我的表单代码,请为:

<script src="js/jquery-1.11.0.js"></script>
<script>
$(document).ready(function(){
    $('#add_client').on('submit',function(e) {
        $.ajax({
          url:'add_client_query.php',
          data:$(this).serialize(),
          type:'POST',
          success:function(data, textStatus) {
            console.log(data);     
            window.location.href = "clients.php";    // Show Success Message
          },
          error:function(data) {
            $("#error").show().fadeOut(5000);    // Show Error Message
          }
        });
        e.preventDefault();    // To Avoid Page Refresh and Fire the Event "Click"
    });
});
</script>

这是HTML表单代码:

<div class="alert alert-danger" id="error" style="display:none;">One Account Is Already Exist There With The Username.Please Try With Unqiue Username.</div>
<form role="form" action="add_client_query.php" method="post" id="add_client">
  <div class="form-group">
    <label>Username :</label>
    <input name="username" class="form-control">
  </div>
  <div class="form-group">
    <label>Password :</label>
    <input name="password" class="form-control">
  </div>
  <div class="form-group">
    <label>Assign Form</label>
    <select class="form-control" name="formlink">
      <?php    // $resultset now holds all rows from the first query.
      foreach ($resultset as $result) {  ?>
      <option value="<?php echo $result['form_name']; ?>"><?php echo ucwords($result['form_name']); ?></option>
      <?php } ?>
    </select>
  </div>
  <div class="form-group">
    <label>Do You Want To Assign Invoice To The Client?</label></br>
    <label><input type="radio" id="yes" name="invoice_choice[]" id="optionsRadios1" value="Yes"> Yes</label>
    <label><input type="radio" id="no" name="invoice_choice[]" id="optionsRadios1" value="No"> No</label>
  </div>
  <div class="invoice_price" style="display:none;">
    <div class="form-group" >
      <label>Invoice Price :</label>
      <input name="invoice_price"  class="form-control">
    </div>
  </div><!-- end div invoice_price -->
  <button type="submit" class="btn btn-default">Submit Button</button>
</form>

下面是用于查询的PHP代码:

<?php
include('../config/config.php');
session_start();
if ($_SESSION['admin']) { 
    if (isset($_POST['username'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $formlink = $_POST['formlink'];
        $invoice_choice = $_POST['invoice_choice'];
        foreach ($invoice_choice as $invoice_choice) 
        { }
        if ($invoice_choice=="Yes") {
            $invoice_price = $_POST['invoice_price'];
            $invoice_status = "Pending";
        } elseif ($invoice_choice=="No") {
            $invoice_price = "0";
            $invoice_status = "Not Assigned";
        }
        $query = mysqli_query($mysqli,"INSERT INTO clientlog  (Username,Password,formlink,invoice_price,invoice_status)
        VALUES ('$username','$password','$formlink','$invoice_price','$invoice_status');") or die(mysqli_connect_error());
    }
} else {
    header('Location:index.php');
} 
?>

所以,如果你们能告诉我哪里做错了什么,那将是一件非常好的事情。

您在PHP代码中使用了"do or die()"构造,这是ajax successerror函数混淆的主要原因。(请注意,这不是一个很好的做法,你可能想查看codereview.SE来改进你的应用程序。)

来自PHP手册:

这个语言构造等价于exit()。

虽然您可以将$status设置为exitdie的参数,但这只是一个告诉PHP代码如何退出的代码,而不是HTTP响应代码(事实上,退出状态必须在[0254]范围内,因此无法获取大部分HTTP响应代码)

如果ajax调用得到指示错误的HTTP响应代码,那么ajax error函数将运行。然而,简单地调用die()将返回状态代码200(指示成功)。您的PHP脚本被成功调用,即使它可能以非零状态退出。

为了更改HTTP响应代码,您有两个选项:

  1. 使用http_response_code函数,可以获取或设置响应代码
  2. 使用header功能手动发送响应代码