tags:

views:

48

answers:

1

Hi All

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

[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 example

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

JQUERY POST example

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

PHP PROCESSING example

<?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? i.e. Instead of sending "Alright" how would I redirect the user to, say a CONFIRMATION page?

+1  A: 

Could you just redirect the user to the success/failure page in 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:

function(data) {
    if (data.validated) { 
        window.location = data.successURL; 
    } else { 
        window.location = data.failureURL; 
    }
}
Joseph Paterson
is this affected if javascript is turned off?
st4ck0v3rfl0w
JQuery won't work full stop if Javascript is disabled.
Joseph Paterson