我的Ajax表单提交不起作用
My Ajax form submit is not working?
我的表单无法工作。即使查询没有像我希望用户名是唯一的那样执行,它也会成功,所以我想在用户名不唯一时触发一个错误,如果成功,我想重定向到一个页面,即"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 success
和error
函数混淆的主要原因。(请注意,这不是一个很好的做法,你可能想查看codereview.SE来改进你的应用程序。)
来自PHP手册:
这个语言构造等价于exit()。
虽然您可以将$status
设置为exit
或die
的参数,但这只是一个告诉PHP代码如何退出的代码,而不是HTTP响应代码(事实上,退出状态必须在[0254]范围内,因此无法获取大部分HTTP响应代码)
如果ajax调用得到指示错误的HTTP响应代码,那么ajax error
函数将运行。然而,简单地调用die()
将返回状态代码200(指示成功)。您的PHP脚本被成功调用,即使它可能以非零状态退出。
为了更改HTTP响应代码,您有两个选项:
- 使用
http_response_code
函数,可以获取或设置响应代码 - 使用
header
功能手动发送响应代码
相关文章:
- 返回 false 不会停止表单提交
- e.preventDefault();不会停止表单提交
- $(id).submit() 不会触发表单提交
- ASP MVC jQuery验证专注于工作,但不专注于表单提交
- 返回false不起作用,单击h:commandButton将触发操作
- 动态创建的文本框不向php表单提交值
- 在JS中返回false,不停止JSP表单提交
- 不能在表单提交上获得选择单选按钮
- $location.path不会更改表单提交后的路线
- 美元.fieldValue不是登录表单提交时的函数警告
- 在尝试将VueJS与SemanticUI集成时,不会阻止表单提交
- Mockjax不拦截异步表单提交
- Safari不读取Jquery表单提交的提交按钮值
- CSS背景图片不会在表单提交后加载
- jquery加载(NBUBNA)不会在表单提交时显示图片
- onFormSubmit触发器不能在表单提交时发送电子邮件
- Safari不会触发表单提交
- 使用时代码不起作用 阻止默认 提交表单
- 为什么代码不起作用(使用 casperjs 提交表单)
- 动画不起作用后单击JQuery