简体   繁体   English

PHP + jQuery-如何成功表单提交,处理和重定向?

[英]PHP + jQuery - How to form submit, process and redirect on success?

Stupid question (I seem to be leading all my questions with this phrase), but I was wondering if anyone out there had an elegant solution for processing forms via php and jquery and then redirects 愚蠢的问题(我似乎用这句话来回答所有问题),但我想知道是否有人在那里有一个优雅的解决方案,可以通过php和jquery处理表单,然后重定向

[HTML FORM] --> submits via jquery post to --> [PHP FILE] --> upon success redirects to --> [SUCCESS HTML PAGE] (and if not success returns back to [HTML FORM] [HTML FORM] -> 通过jquery提交到 -> [PHP FILE] -> 成功后重定向到 -> [SUCCESS HTML PAGE] (如果不成功,则返回[HTML FORM]

HTML FORM example HTML FORM示例

<form action="procRegister.php" method ="post" id="registerAccount" name="registerAccount">
...fields here ladeeda...
</form>

JQUERY POST example JQUERY POST示例

$("form#registerAccount").submit(function(){
    $.post(
        "procRegister.php",
        $("form#registerAccount").serialize(),
        function(data){alert(data.msg);}, 
        'json'
           );   
    return false;
});

PHP PROCESSING example PHP处理示例

<?php
    header('Content-type: application/json');
    $return['msg'] = 'Alright!';
    echo json_encode($return);
?>

If everything works correctly then the page would pop up an alert box saying "Alright!". 如果一切正常,则该页面将弹出一个警告框,提示“好!”。 Now, how and where would I implement the redirect upon success? 现在,成功后我将如何以及在何处实施重定向? ie Instead of sending "Alright" how would I redirect the user to, say a CONFIRMATION page? 即我不是发送“好的”,而是将用户重定向到“确认”页面?

Could you just redirect the user to the success/failure page in javascript? 您能否将用户重定向到javascript中的成功/失败页面?

window.location = <url>

If you need some sort of token to access your success page you could possibly pass the URL back in the JSON that is returned by your procRegister.php page: 如果您需要某种令牌来访问成功页面,则可以将URL传递回procRegister.php页面返回的JSON中:

function(data) {
    if (data.validated) { 
        window.location = data.successURL; 
    } else { 
        window.location = data.failureURL; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM