Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理来自ajax post的结果_Ajax_Jquery - Fatal编程技术网

处理来自ajax post的结果

处理来自ajax post的结果,ajax,jquery,Ajax,Jquery,我有一个设置表单,我想确保用户不会错误地进行更改。用户完成后,他们将单击保存按钮,弹出一个模式(引导模式),提示用户输入密码以继续处理原始表单 如果输入的密码正确,则提交第一个表单,否则页面将重新加载/重定向回自身。第一个表单将包含许多字段,但我只是使用其中一个进行测试 <form action="http://localhost.testing/render_confirm_with_password.php" method="POST" id="validation-form" cla

我有一个设置表单,我想确保用户不会错误地进行更改。用户完成后,他们将单击保存按钮,弹出一个模式(引导模式),提示用户输入密码以继续处理原始表单

如果输入的密码正确,则提交第一个表单,否则页面将重新加载/重定向回自身。第一个表单将包含许多字段,但我只是使用其中一个进行测试

<form action="http://localhost.testing/render_confirm_with_password.php" method="POST" id="validation-form" class="form-horizontal confirm-form">
    <div class="form-group">
        <label for="deletion_reason" class="col-xs-3 col-lg-4 control-label">Enter Your Value</label>
        <div class="col-sm-9 col-lg-6 controls">
            <input type="text" name="value" placeholder="yourname@email.com" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-9 col-sm-offset-3 col-lg-6 col-lg-offset-4">
            <!-- <button type="submit" class="btn btn-primary"><i class="icon-ok"></i> Process</button> -->
            <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#password_confirm_modal" data-modal-type="password_confirm" data-modal-title="Delete Transaction" data-modal-text="Are you sure you want to delete this transaction?"><i class="icon-ok"></i> Process</button>
        </div>
    </div>
</form>
如果密码不匹配,我尝试从PHP内部重定向,但它仍然处理表单1。 别担心,它的硬编码只是为了测试

$password = "password";

if(isset($_POST['password'])){
    $pw = $_POST['password'];
    if($password == $pw){
        // process form_1
        echo "MATCH";
    }else{
        // return to form_1
        header('Location: http://localhost.testing/confirm_with_password.html');
        echo "NO MATCH";
    }
}else{
    // return to form_1
    echo "NOT SET";
}   

PHP代码中处理AJAX请求的头函数所做的是,它只重定向AJAX请求,而不重定向浏览器中的页面。删除重定向,因为它不会返回到表单1

如果您想在密码输入错误时关闭引导模式框,可以调用
$(“#password_confirm_modal”).modal(“hide”)
位置.重新加载()
从else语句中的AJAX回调中刷新/重新加载页面,在该语句中处理错误的密码响应,但使用后者将丢失输入表单的所有数据

您可以在模式框中显示密码输入错误的警告,并让用户再次尝试输入密码,而不是执行这两种操作中的任何一种。至于安全方面,您可能希望在提交之前将正确的密码作为隐藏字段添加到原始表单中,以便在保存实际数据时可以验证密码

$('#password_confirm_submit').click(function(e){
    var password = $('#password').val();
    e.preventDefault();
    $.ajax({
        type: 'POST',
        data: {password: password},
        url: 'http://localhost.testing/process_confirm_with_password.php',
        success: function(result){
            $('#password_result').html(result);
            if(result == "MATCH"){
                // process form_1
                $('.confirm-form').submit();
            }else{
                // back to form_1
            }
        }
    })
});
$password = "password";

if(isset($_POST['password'])){
    $pw = $_POST['password'];
    if($password == $pw){
        // process form_1
        echo "MATCH";
    }else{
        // return to form_1
        header('Location: http://localhost.testing/confirm_with_password.html');
        echo "NO MATCH";
    }
}else{
    // return to form_1
    echo "NOT SET";
}