[英]Didn't understand how success function works in AJAX
This question been asked a lot here, but after spending almost half of my day, I couldn't solve my issue.这个问题在这里被问了很多,但是在花了将近半天的时间后,我无法解决我的问题。
I have this form in user profile where user can change the password.我在用户配置文件中有此表单,用户可以在其中更改密码。 I validate form with JS and send it to PHP to process.
我使用 JS 验证表单并将其发送到 PHP 进行处理。 However at the end of PHP script I check if it makes successfull connection to database and completes the process and I echo 1 in this case.
但是,在 PHP 脚本结束时,我检查它是否成功连接到数据库并完成该过程,在这种情况下我回显 1。 I want to inform user in JS but it works opposite way.
我想在 JS 中通知用户,但它的工作方式相反。 I mean when result == 0
我的意思是当结果 == 0
HTML HTML
<form id="passwordChangeForm">
<input type="hidden" id="uid" name="uid" class="form-control" value="<?= ($user_detail['id']) ? $user_detail['id'] : '' ?>">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="password1">Password</label><br>
<input type="password" name="password" id="password1">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="password1">Password Repeat</label><br>
<input type="password" name="passwordr" id="passwordr">
</div>
</div>
<button type="submit" id="pwd-change-btn" onclick = "pwdChange()">Submit</button><br><br>
<p id="pwd-change-error"></p>
</div>
</form>
JavaScript JavaScript
function pwdChange(){
var pwd1 = $('#password1').val();
var pwdr = $('#passwordr').val();
var uid = $('#uid').val();
var form = $('#passwordChangeForm');
$('#pwd-change-error').empty();
if(pwd1.length < 8 || pwdr.length < 8){
form.submit(function(e){
e.preventDefault();
});
$("#pwd-change-error").append('<div class="alert alert-danger"><button class="close" data-close="alert"></button>Passwrod must be at least 8 characters</div>').delay(3000).fadeOut(3000);
} else if(pwd1 !== pwdr){
form.submit(function(e){
e.preventDefault();
});
$("#pwd-change-error").append('<div class="alert alert-danger"><button class="close" data-close="alert"></button>Passwrods doesn\'t match</div>');
} else{
var updatePwd = '';
$.ajax({
url: "update.php",
type: "POST",
data:{
pwd1: pwd1,
uid: uid,
updatePwd: updatePwd,
},
sucess: function(){
if (result == '1') {
window.location = "index.php";
} else {
$("#pwd-change-error").append('<div class="alert alert-danger"><button class="close" data-close="alert"></button>Error accoured</div>');
}
}
});
}
}
PHP PHP
<?php
session_start();
require_once "db_connect.php";
if (isset($_POST['updatePwd'])) {
$pwd = $_POST['pwd1'];
$uid = $_POST['uid'];
$sql = "UPDATE users SET password = ? WHERE id = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "Something went wrong :(";
exit();
} else {
$newPwdHash = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ss", $newPwdHash, $uid);
mysqli_stmt_execute($stmt);
$query = mysqli_query($conn, $sql);
}
if ($query == 1) {
echo '1';
} else {
echo '0';
}
}
JSON parameters are not set and the response of success have no identification
未设置JSON参数,成功响应无标识
You should do it like this:你应该这样做:
data:{ // data is not a valid JSON
"pwd1": pwd1,
"uid": uid,
"updatePwd": updatePwd,
}
For Redirection:对于重定向:
success : function(result){
if (result == '1') {
window.location.href = "index.php";
} else {
$("#pwd-change-error").append('<div class="alert alert-danger"><button class="close" data-close="alert"></button>Error accoured</div>');
}
}
"undefined index" error occurs when the key you are referencing is not found in an array.当在数组中找不到您引用的键时,会发生“未定义索引”错误。 The problem is here:
问题在这里:
$.ajax({
url: "update.php",
type: "POST",
data:{ // data is not a valid JSON
pwd1: pwd1,
uid: uid,
updatePwd: updatePwd,
},
success: function(){
if (result == '1') {
window.location = "index.php";
} else {
$("#pwd-change-error").append('<div class="alert alert-danger"><button class="close" data-close="alert"></button>Error accoured</div>');
}
}
});
since data is not a valid JSON POST parameters are not set.由于数据不是有效的 JSON POST 参数未设置。 You should do it like this:
你应该这样做:
data:{ // data is not a calid JSON
"pwd1": pwd1,
"uid": uid,
"updatePwd": updatePwd,
}
js中的重定向原因是:
window.location.href "index.js";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.