使用ajax结果进行url重定向

时间:2012-07-15 14:35:53

标签: php ajax

我希望在使用AJAX验证表单后,它会被重定向到另一个页面,但我的编码显示结果为specfic <div id="result">。我使用以下代码在index.php上获得此结果:

<div class="c_right">
    <div>
        <div class="t_h_c">
            Hi Guest
        </div>
        <div id="result" class="error">
        </div>
        <div class="username">
            Username:
        </div>
        <form name="signin" id="signin" method="post">
            <div class="f_l2">
                <input type="text" class="form_username" name="username" maxlength="40">
            </div>
            <div style="clear:both;">
            </div>
            <div class="username">
                Password:
            </div>
            <div class="f_l2">
                <input type="password" class="form_username" name="pass" maxlength="50">
            </div>
            <div style="clear:both;">
            </div>
            <div class="username"></div>
            <div class="f_l2">
                <a href="javascript:void(0)" onclick="document.getElementById('light3').style.display='block';document.getElementById('fade3').style.display='block'">
                    <img src="img/jpg/sinup.png" />
                </a>
            </div>
            <div class="f_r">
                <img src="img/jpg/sin-in.png" onClick="showHint()" />
        </form>
    </div>
</div>
</div>
<script type="text/javascript">
    // function create GetXmlHttpObject
    function showHint()
    {
        var t2lFname=document.signin.username.value;
        var t2lLname=document.signin.pass.value;
        var parameters="firstname="+t2lFname+"&lastname="+t2lLname;
        var xmlhttp;

        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState == 1)
            {
                document.getElementById("chart").innerHTML ="<img src='img/ajax-loader.gif' />";
            }
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("result").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("POST","thank-you.php?username="+t2lFname+"&pass="+t2lLname,true);
        xmlhttp.send();
    }
</script>

以下是thank-you.php的代码。我希望在验证之后,index.php会在验证members.php结果显示为resulty id之后立即转移到members.php。

<?php
    echo $_REQUEST['username'];
    echo $_REQUEST['pass'];
    include("connection.php");

    $query = mysql_query("SELECT COUNT(*) FROM user");
    list($number) = mysql_fetch_row($query);

    //Checks if there is a login cookie
    //if the login form is submitted
    if (isset($_REQUEST['username'])) { // if form has been submitted
        // makes sure they filled it in
        if(!$_REQUEST['username'] | !$_REQUEST['pass']) {
            die ('<span class="eror">You did not fill in a required field.</span>');
        }

        // checks it against the database
        if (!get_magic_quotes_gpc()) {
            $_REQUEST['email'] = addslashes(@$_REQUEST['email']);
        }

        $check = mysql_query("SELECT * FROM user WHERE un = '".$_REQUEST['username']."'")or die(mysql_error());
        //Gives error if user dosen't exist
        $check2 = mysql_num_rows($check);

        if ($check2 == 0) {
            die ("User name does not exist");
        }

        while($info = mysql_fetch_array( $check ))
        {
            $_REQUEST['pass'] = stripslashes($_REQUEST['pass']);
            $info['password'] = stripslashes($info['password']);
            $_REQUEST['pass'] = md5($_REQUEST['pass']);

            //gives error if the password is wrong
            if ($_REQUEST['pass'] != $info['password']) {
                die ('<span class="eror">Incorrect password,<i> please try again.</i></span>');
            }
            else
            {
                // if login is ok then we add a cookie
                $_REQUEST['username'] = stripslashes($_REQUEST['username']);
                $hour = time() + 600000000000000000000000000;
                setcookie(ID_my_site, $_REQUEST['username'], $hour);
                setcookie(Key_my_site, $_REQUEST['pass'], $hour);
                include("time.php");
                $conform=mysql_query("INSERT INTO user_ts (user_ts_user,user_ts_uli,user_ts_date, user_ts_umd) VALUES ('$_REQUEST[username]','$x','$d','$time_code')");

                if ($conform)
                    {
                        //then redirect them to the members area
                        setcookie('md',$time_code);
                        setcookie('li',$x);
                        setcookie('h',$h);
                        setcookie('m',$m);
                        # index.php is moved to members.php
                        header("Location: members.php");
                    }
                    else
                    {
                        die ('Not Inserted');
                    }
                }
            }
        }
        else
        {
            // if they are not logged in
        }
    }
?> 

1 个答案:

答案 0 :(得分:0)

我认为您可以使用window.location

而不是:

xmlhttp.open("POST","thank-you.php?username="+t2lFname+"&pass="+t2lLname,true); 
xmlhttp.send(); 

使用window.location:

window.location("thank-you.php?username="+t2lFname+"&pass="+t2lLname);

使用xmlhttp调用“thank-you.php”将导致index.php接收到页面的响应,并且不会告诉浏览器去那里。