使用AJAX,Jquery和PHP的多页表单

时间:2011-12-13 20:56:43

标签: php jquery ajax json

我目前正在尝试创建一个多页面表单,它同时使用jQuery和PHP进行错误检查。我遇到的问题是我对jQuery相对较新,并且不知道如何转移到第二个表单而不是仅显示来自PHP数组的成功消息。

我使用了(http://www.youtube.com/watch?v=BwExOhVaUyk)的例子作为我的向导。它是一个使用ajax_submit在表单就绪时进行处理的示例,如果成功则运行PHP脚本并进行错误检查,然后返回成功的消息。

我不想要成功的消息。我想要成功转到第二页,但不知道如何做到这一点。

The following is the ajax code

// make our ajax request to the server
function submitForm(formData) {
    $.ajax({    
        type: 'POST',
        url: 'feedback.php?images=' + encs.toString(),      
        data: formData,
        dataType: 'json',
        cache: false,
        timeout: 7000,
        success: function(data) {           

            if(data.error == true){

            $('form #response').removeClass().addClass('error').html(data.msg).fadeIn('fast');                                      

            } else{
                $("#rightcolumn").fadeOut(500, function(){

                    //NOTE THIS IS THE LINE THAT BRINGS BACK THE SUCCESSFUL MESSAGE STORED IN data.msg (taken from array in php file).

                    $("#rightcolumn").append().html(data.msg).fadeIn(500);
                });

            }



        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            $('form #response').removeClass().addClass('error')
                        .html('<p>There was an<strong> ' + errorThrown +
                              '</strong> error due to a<strong> ' + textStatus +
                              '</strong> condition.</p>').fadeIn('fast');           
        }

PHP

$return['error'] = false;
            $return['msg'] = "<html>";
            $return['msg'] .="<head>"; 
            $return['msg'] .="</head>"; 
            $return['msg'] .= "<div id=\"coverrightb\">";
            $return['msg'] .= "<body>";
            $return['msg'] .= "<h2>Successful Registration <img id=\"thumbsup\" src=\"images/icons/thumbsup.png\" alt=\"ThumbsUp\" /></h2>";
            $return['msg'] .= "<br />"; 
            $return['msg'] .= "<p>Thanks for registering " .$firstname .". Your details are below.</p>";
            $return['msg'] .= "<br />"; 
echo json_encode($return);

我希望它只是转到第二页,不知道如何做到这一点。非常感谢一些指导。我正在学习PHP和jQuery,但已经坚持了几个星期。

非常感谢

2 个答案:

答案 0 :(得分:0)

我不太清楚你想要什么,但如果你只是想用另一个表单重定向到另一个页面,那么在ajax请求的回调中,使用window.location javascript重定向。

答案 1 :(得分:0)

您可以通过将window.location更改为所需的网址来重定向用户:

    success: function(data) {           
        if(data.error == true){
            $('#response').removeClass().addClass('error').html(data.msg).fadeIn('fast');
        } else{
            $("#rightcolumn").fadeOut(500, function(){

                //NOTE THIS IS THE LINE THAT BRINGS BACK THE SUCCESSFUL MESSAGE STORED IN data.msg (taken from array in php file).
                window.location = 'form-page-2.php';//note that this line will stop running the JavaScript on this page, so the next line will not run

                $("#rightcolumn").append().html(data.msg).fadeIn(500);
            });

        }
    },

另一种解决方案是通过AJAX将新表单加载到同一页面中,这样用户就不必加载一个全新的页面:

    success: function(data) {           
        if(data.error == true){
            $('#response').removeClass().addClass('error').html(data.msg).fadeIn('fast');
        } else{
            $("#rightcolumn").fadeOut(500, function(){

                //NOTE THIS IS THE LINE THAT BRINGS BACK THE SUCCESSFUL MESSAGE STORED IN data.msg (taken from array in php file).

                $("#rightcolumn").load('form-page-2.php #only-the-form', function () {
                    $(this).fadeIn(500);
                });
            });

        }
    },

ya的一些文档: