0

因此,我正在尝试实现基于 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>

查询:

$('#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但没有任何效果。我不明白为什么它看不到文件夹。有任何想法吗?

4

2 回答 2

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
});
于 2012-10-20T23:00:42.867 回答
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..
            //$(

您的网址字段为空

于 2012-10-20T21:10:49.423 回答