当我点击按钮加载js文件时,没有任何反应,我已经单独测试了php文件而没有ajax只是纯PHP而且工作正常!所以我认为这里的代码存在问题:
$(document).ready(function(){
$('#changePasswordNeededButton').click(function(){
$.post("http://example.com/change_password.php",
{
currentPassword : $('#currentPassword').val(),
newPassword : $('#newPassword').val(),
repeatNewPassword : $('#repeatNewPassword').val()
},
function(data){
if (data.result == 1) {
location.href = data.location;
}
}, "json" // get content which has been returned in json format
);
});
});
HTML for this:
<div class='changePassword'>
<div id='changePasswordInner'>
<form>
<input id='currentPassword' type='password' name='currentPassword' placeholder='Nuværende adgangskode'>
<input id='newPassword' type='password' name='newPassword' placeholder='Ny adgangskode'>
<input id='repeatNewPassword' type='password' name='repeatNewPassword' placeholder='Gentag ny adgangskode'>
<input id='changePasswordNeededButton' type='button' name='changePasswordNeededButton' value='Skift adgangskode'>
</form>
<div id'errorChangePassword'></div>
</div>
</div>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="../script/change_password.js"></script>
我的php:
if (isset($_POST['currentPassword']) && isset($_POST['newPassword']) && isset($_POST['repeatNewPassword']) {
$currentPassword = $_POST['currentPassword'];
$newPassword = $_POST['newPassword'];
$repeatNewPassword = $_POST['repeatNewPassword'];
$query = "SELECT * FROM user WHERE password = '$currentPassword'";
$result = $db->query($query);
while ($row = $result->fetch_assoc()) {
$password = $row['password'];
}
if ($password == $currentPassword) {
$update = "UPDATE user SET password = '$newPassword' WHERE password = '$currentPassword'";
$db->query($update);
$result = array(
'result' => 1,
'location' => 'index.php',
);
}
// return the result
echo json_encode($result);
die();
}
我希望有人能在这里看到错误吗?
谢谢..
答案 0 :(得分:2)
看一下jQuery的$ .post文档。我实际上会争论使用$ .ajax而不是因为它带来了额外的功能;主要是为成功和失败添加回调的能力。
来自jQuery的AJAX文档:
..
答案 1 :(得分:0)
检查此行
'location' => 'index.php',
您的代码不适用于额外的, at the end of that line
。由于您的脚本需要json
并且收到格式错误的json
字符串。
<?php
if (isset($_POST['currentPassword']) && isset($_POST['newPassword']) && isset($_POST['repeatNewPassword'])) {
$result = array(
'result' => 1,
'location' => 'index.php'
);
echo json_encode($result);
exit();
};
?>
<form>
<input id='currentPassword' type='password' name='currentPassword' placeholder='Nuværende adgangskode'>
<input id='newPassword' type='password' name='newPassword' placeholder='Ny adgangskode'>
<input id='repeatNewPassword' type='password' name='repeatNewPassword' placeholder='Gentag ny adgangskode'>
<input id='changePasswordNeededButton' type='submit' name='changePasswordNeededButton' value='Skift adgangskode'>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#changePasswordNeededButton').click(function(){
$.post("",
{
currentPassword : $('#currentPassword').val(),
newPassword : $('#newPassword').val(),
repeatNewPassword : $('#repeatNewPassword').val()
},
function(data){
if (data.result == 1) {
location.href=data.location;
}
},"json"
);
return false;
});
});
</script>