不了解AJAX中的成功功能如何工作

时间:2019-08-07 15:11:17

标签: javascript php html ajax

这个问题在这里被问了很多,但是花了将近一天的时间之后,我无法解决我的问题。

我在用户个人资料中有此表单,用户可以在其中更改密码。我使用JS验证表单并将其发送到PHP进行处理。但是,在PHP脚本结束时,我检查它是否成功连接到数据库并完成了该过程,在这种情况下,我回显1。我想在JS中通知用户,但它的工作方式相反。我的意思是当结果== 0

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

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

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';
    }
}

3 个答案:

答案 0 :(得分:0)

  

未设置JSON参数,并且没有成功响应

您应该这样做:

data:{  // data is not a valid JSON
        "pwd1": pwd1, 
        "uid": uid,
        "updatePwd": updatePwd,
   }

用于重定向:

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>');
        }
    }

答案 1 :(得分:-1)

当在数组中找不到要引用的键时,会出现

“未定义索引” 错误。问题在这里:

$.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>');
            }
        }
    });

由于数据无效,因此未设置JSON POST参数。您应该这样:

data:{  // data is not a calid JSON
            "pwd1": pwd1, 
            "uid": uid,
            "updatePwd": updatePwd,
        }

答案 2 :(得分:-1)

js中的重定向原因是:

window.location.href "index.js";