Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的Ajax表单提交不起作用?_Javascript_Php_Jquery_Ajax_Forms - Fatal编程技术网

Javascript 我的Ajax表单提交不起作用?

Javascript 我的Ajax表单提交不起作用?,javascript,php,jquery,ajax,forms,Javascript,Php,Jquery,Ajax,Forms,我的表格坏了。即使查询没有像我希望用户名唯一一样执行,它也会在上成功,因此我希望在用户名不唯一时触发一个错误,如果成功,我希望重定向到一个页面,即“clients.php”,因此这是我的表单代码,请如下所示: <script src="js/jquery-1.11.0.js"></script> <script> $(document).ready(function(){ $('#add_client').on('submit',function(e)

我的表格坏了。即使查询没有像我希望用户名唯一一样执行,它也会在上成功,因此我希望在用户名不唯一时触发一个错误,如果成功,我希望重定向到一个页面,即“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>

$(文档).ready(函数(){
$('add#u client')。关于('submit',函数(e){
$.ajax({
url:'add_client_query.php',
数据:$(this).serialize(),
类型:'POST',
成功:功能(数据、文本状态){
控制台日志(数据);
window.location.href=“clients.php”;//显示成功消息
},
错误:函数(数据){
$(“#error”).show().fadeOut(5000);//显示错误消息
}
});
e、 preventDefault();//要避免页面刷新并触发事件“单击”
});
});
以下是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>
已存在一个用户名为的帐户。请尝试使用Unqiue用户名。
用户名:
密码:
指定表格
您在PHP代码中使用的是“do or
die()
”结构,这是对ajax
success
error
函数产生混淆的主要原因。(请注意,这不是一个很好的实践,您可能希望查看codereview.SE以改进您的应用程序。)

从PHP手册:

此语言构造相当于exit()

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

如果ajax调用获得指示错误的HTTP响应代码,则ajax
error
函数将运行。但是,只需调用
die()
将返回状态代码200(表示成功)。您的PHP脚本被成功调用,即使它可能以非零状态退出

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

  • 使用
    http\u response\u code
    函数,可以获取或设置响应代码
  • 使用
    标题
    功能手动发送响应代码

  • 这段代码非常糟糕,而且充满了糟糕的实践。我真的是一个开发的初学者。。!任何帮助都会很好。。!请找出我到底做错了什么..我希望我可以,但我甚至看不懂你写的东西它很简单所有其他的事情都很好,因为现在ajax不起作用它成功了,即使没有执行查询。。!相反,它应该触发该错误消息。。!
    <?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');
    } 
    ?>