所以我正在尝试实现基于ajax的密码更改,以便在用户点击提交时不刷新页面。所以这是我的html,jquery和php:
Current Password<input type="password" id="change_password" name="change_password"><br>
New Password<input type="password" id="new_password" name="new_password"><br>
Verify Password<input type="password" id="verify_password" name="verify_password"><br>
的Jquery:
$('#change_Pass').submit(function(e){
var $this = $(this);
$.ajax({
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: '/Private/change_password.php', // the file to call
success: function(response) { // on success..
//$('#success_div).html(response); // update the DIV
alert("good");
},
error: function(e, x, r) { // on error..
//$('#error_div).html(e); // update the DIV
alert("bad");
}
});
e.preventDefault();
return false; //so it doesn't refresh or submit the page
});
PHP:
<?php
session_start();
require_once '../classes/Bcrypt.php';
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
$usr = $_SESSION["username"];
$old_pwd = $_POST["change_password"];
$new_pwd = $_POST["new_password"];
$new_pwd = Bcrypt::hash($new_pwd);
try {
$link = new PDO('mysql:host=*;dbname=*;charset=UTF-8','*','*');
$query = "SELECT *
FROM Conference
WHERE Username = :un";
$stmt = $link->prepare($query);
$stmt->bindParam(':un', $usr);
$stmt->execute();
$row = $stmt->fetchAll();
$hash = $row[0]["Password"];
$is_correct = Bcrypt::check($old_pwd, $hash);
if($is_correct) {
$query = "UPDATE Conference
SET `Password`=:new_pwd
WHERE Username = :usr";
$stmt = $link->prepare($query);
$stmt->bindParam(':new_pwd', $new_pwd);
$stmt->bindParam(':usr', $usr);
$stmt->execute();
}
} catch(PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
我的文件结构只是:
Folder
|--Front.php
|--Private
|--change_password.php
但每次我输入内容并点击提交。它会尝试转到www.domain.com/Folder/change_password.php
而不是www.domain.com/Folder/Private/change_password.php
。它只是说
The requested URL /Folder/change_password.php was not found on this server.
我已经尝试了完整的网址,只是/Private/change_password.php
并且没有任何效果。我不明白为什么它看不到文件夹。有什么想法吗?
答案 0 :(得分:0)
$.ajax({
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: '/', // the file to call
success: function(response) { // on success..
//$(
您的网址字段为空
答案 1 :(得分:0)
使用$ this而不是$(this)并再试一次:
$('#change_Pass').submit(function(e){
var $this = $(this);
$.ajax({
data: $this.serialize(), // $this
type: $this.attr('method'), // $this
url: '/Private/change_password.php', //
success: function(response) {
alert("good");
},
error: function(e, x, r) {
alert("bad");
}
});
e.preventDefault();
return false; //so it doesn't refresh or submit the page
});