带有ajax结果的url重定向


url redirection with ajax result

我希望在用AJAX验证表单后,它将被重定向到另一个页面,但我的编码在特定的<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>

下面是感谢.php的代码。我希望在验证后index.php将移动到members.php,现在在验证后members.hp结果显示为resulty.

<?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
        }
    }
?> 

我认为您可以使用window.location

代替:

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

使用窗口。位置:

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

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