没有从PHP回调到Ajax

时间:2014-11-30 00:31:54

标签: php jquery ajax

我有一个小网页来更改当前密码。当用户输入旧凭据和新密码时。单击“提交”按钮。 Ajax调用将详细信息提交给后端PHP脚本。

当我点击提交按钮时,新密码将更新到数据库,但我没有获得回调成功和警报。我的代码有问题吗?

这是JavaScript。

$('#submit').click( 
        function() 
        {
            getLoggedInUser();
            var email = $('#loginuser').val();
            var oldPassword = $('#oldpassword').val();
            var newPassword = $('#newpassword').val();
            $.ajax({
                    type: "POST",
                    url:  "saveuserpassword.php",
                    data:{
                            email: email, 
                            oldPassword: oldPassword, 
                            newPassword: newPassword
                         },
                    dataType: "text",
                    success: function(data) {
                        alert(data);
                    }
                  });
        }
    );

以下是更改密码的PHP代码。

<?php
require_once('includes/config.inc.php');
$updateSuccess = FALSE;
function updatePassword($email, $newpwd) {
    $con=mysql_connect(DB_HOSTNAME, DB_USERNAME,DB_PASSWORD);  
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    } 
    mysql_select_db(DB_DATABASE,$con);
    $updateQuery = "update employees set password = '". md5($newpwd). "' where email = '". $email . "'" ;
    $result=mysql_query($updateQuery,$con); 
    if( $result == TRUE )
        return TRUE;
    else
        return FALSE;
}

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $email = trim($_POST['email']);
    $oldPassword = trim($_POST['oldPassword']);
    $newPassword = trim($_POST['newPassword']);

    $con=mysql_connect(DB_HOSTNAME, DB_USERNAME,DB_PASSWORD);  
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    } 
    mysql_select_db(DB_DATABASE,$con);

    $query = "select * from employees where email = '" . $email. "' and password = '" . md5($oldPassword). "'";
    $result = mysql_query($query, $con);
    if( $data = mysql_fetch_row($result) )
    {
        if( updatePassword($email, $newPassword) )
            $updateSuccess = TRUE;
    }
    else
    {
        $updateSuccess = FALSE;
    }
    if( $updateSuccess )
        echo "Password updated successfully!";
    else
        echo "Unable to update password";
}
?>

0 个答案:

没有答案