简体   繁体   English

标头不会重定向到另一个页面ajax php

[英]header doesn't redirect to another page ajax php

I'm working on login form request using ajax concept. 我正在使用ajax概念处理登录表单请求。 Below i've posted my codes related to login. 在下面,我发布了与登录相关的代码。 It doesn't redirect to myaccount.php page. 它不会重定向到myaccount.php页面。 if i use this echo'<script>window.location="myaccount.php";</script>'; 如果我使用此echo'<script>window.location="myaccount.php";</script>'; javascript function it will work properly. javascript函数,它将正常工作。 But, here i want to do with php. 但是,在这里我想用PHP。 it returns the myaccount.php results as a ajax message. 它将ajax消息返回myaccount.php结果。

错误图片

I'm getting the error message using this <div id="loginreturn"></div> . 我使用此<div id="loginreturn"></div>收到错误消息。 Above I've posted an error image. 上面,我发布了错误图片。 myaccount.php page results also shows inside the <div id="loginreturn"></div> . myaccount.php页面结果还显示在<div id="loginreturn"></div> That is my problem. 那是我的问题。 it doesn't redirect to another php page. 它不会重定向到另一个PHP页面。 How to solve this error? 如何解决这个错误?

login.php login.php

<?php
    ob_start();
    include('config.php');
    error_reporting(E_ALL | E_WARNING | E_NOTICE);
    ini_set('display_errors', TRUE);
    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
        $user_email = $_POST['email'];
        $user_password = $_POST['password'];    
        $_SESSION['ses_uemail'] = $user_email;
        if(empty($user_email) || empty($user_password) )
        {
            echo "You must provide your email id and password";
            die();
        }       
        try
        {
            $stmt = $conn->prepare("SELECT * FROM table_name WHERE EmailID = ? AND Password = ?");
            $conn->errorInfo();
            $stmt->bindParam('1', $user_email, PDO::PARAM_STR);
            $stmt->bindParam('2', $user_password, PDO::PARAM_STR);
            $stmt->execute();
            while($row = $stmt->fetch())
            {
                $rename = $row['Name'];
                $reemail = $row['EmailID'];
                $repassword = $row['Password'];
            }       
            if($reemail == $_SESSION['ses_uemail'] && $repassword == $user_password)
            {
                $_SESSION['ses_name'] = $rename;
                $_SESSION['ses_email'] = $reemail;
                $_SESSION['ses_password'] = $repassword;
                header('Location:myaccount.php');
            }   
            else
            {
                echo "Incorrect email id and password";
            }
        }
        catch(PDOException $e)
        {
            'Error : ' .$e->getMessage();
        }
    }
    ob_flush();     
?>

ajax.js ajax.js

// ajax signin
    jQuery(document).ready(function() {
        jQuery("#registertologin").click(function() {

                jQuery("#loginreturn").html("<img src='img/loading.gif'/>");
                var postData = $(this).serializeArray();
                var formURL = $(this).attr("action");
                $.ajax( {
                    url : 'login.php',
                    type: "POST",
                    data :  $('#loginform').serialize(),
                    success:function(data, textStatus, jqXHR) {
                        jQuery("#loginreturn").html('<pre><code class="returndata">'+data+'</code></pre>');
                        $("#loginform").submit_login(); //SUBMIT FORM
                    },
                    error: function(jqXHR, textStatus, errorThrown) 
                    {
                        $("#loginreturn").html('<pre><code class="returndata">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
                    }
                });
                e.preventDefault();  //STOP default action
            });
    });

index.php index.php

<form name="loginform" id="loginform" >
        <table class="outlineborder" width="500" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="35" colspan="2">
                <div class="signinform">SignIn Form</div>
            </td>
          </tr>
          <tr>
            <td height="35" colspan="2">&nbsp;</td>
          </tr>
          <tr>
            <td width="150" height="35" class="rightalign">Email Id : </td>
            <td width="348"><input class="inputfield" type="text" name="email" /></td>
          </tr>
          <tr>
            <td height="35" class="rightalign">Password : </td>
            <td><input class="inputfield" type="password" name="password" /></td>
          </tr>
          <tr>
            <td height="35" colspan="2" align="right">
                <span class="forgetpassword">Forget Password ?</span>
            </td>
          </tr>
          <tr>
            <td height="35" colspan="2" align="center">
                <div id="loginreturn"></div>
            </td>
          </tr>
          <tr>
            <td height="35" colspan="2" align="center">
                <input class="btns" type="button" name="submit_login" id="registertologin" value="SignIn" />
                <input class="btns" type="button" value="Close" onclick="document.getElementById('login_signup').style.display='none'; document.getElementById('fade').style.display='none'" />
            </td>
          </tr>
        </table>
    </form>

Changes in 'login.php' 'login.php'中的更改

while($row = $stmt->fetch())
 {
      $rename = $row['Name'];
      $reemail = $row['EmailID'];
      $repassword = $row['Password'];
 }       
 if($reemail == $_SESSION['ses_uemail'] && $repassword == $user_password)
 {
      $_SESSION['ses_name'] = $rename;
      $_SESSION['ses_email'] = $reemail;
      $_SESSION['ses_password'] = $repassword;
      return true;
 }else
 {
      return false;
 }   

Changes in 'ajax.js' 'ajax.js'中的更改

success:function(data, textStatus, jqXHR) {
if(data == true)
{
     window.location.href="myaccount.php"
}else
{
     jQuery("#loginreturn").html('<pre><code class="returndata">'+data+'</code></pre>');
     $("#loginform").submit_login(); //SUBMIT FORM
}

},

Maybe Its Help You... 也许可以帮助您...

Thanks 谢谢

Try this, 尝试这个,

login.php login.php

<?php
    ob_start();
    include('config.php');
    error_reporting(E_ALL | E_WARNING | E_NOTICE);
    ini_set('display_errors', TRUE);
    $msg        =   array();

    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
        $user_email = $_POST['email'];
        $user_password = $_POST['password'];    
        $_SESSION['ses_uemail'] = $user_email;
        if(empty($user_email) || empty($user_password) )
        {
            echo "You must provide your email id and password";
            die();
        }       
        try
        {
            $stmt = $conn->prepare("SELECT * FROM table_name WHERE EmailID = ? AND Password = ?");
            $conn->errorInfo();
            $stmt->bindParam('1', $user_email, PDO::PARAM_STR);
            $stmt->bindParam('2', $user_password, PDO::PARAM_STR);
            $stmt->execute();
            while($row = $stmt->fetch())
            {
                $rename = $row['Name'];
                $reemail = $row['EmailID'];
                $repassword = $row['Password'];
            }       
            if($reemail == $_SESSION['ses_uemail'] && $repassword == $user_password)
            {
                $_SESSION['ses_name'] = $rename;
                $_SESSION['ses_email'] = $reemail;
                $_SESSION['ses_password'] = $repassword;
                //header('Location:myaccount.php'); //no need
                return json_encode(array('error'=> false));

            }   
            else
            {
                //echo "Incorrect email id and password";
                return json_encode(array('error'=> true, 'msg'=> "Incorrect email id and password"));
            }
        }
        catch(PDOException $e)
        {
            //'Error : ' .$e->getMessage();
            return json_encode(array('error'=> true, 'msg'=> $e->getMessage()));
        }
    }
    ob_flush();     
?>

ajax.js ajax.js

// ajax signin
    jQuery(document).ready(function() {
        jQuery("#registertologin").click(function() {

                jQuery("#loginreturn").html("<img src='img/loading.gif'/>");
                var postData = $(this).serializeArray();
                var formURL = $(this).attr("action");
                $.ajax( {
                    url : 'login.php',
                    type: "POST",
                    data :  $('#loginform').serialize(),
                    dataType:"json",
                    success:function(data, textStatus, jqXHR) {
                         jQuery("#loginreturn").html('<pre><code class="returndata">'+data+'</code></pre>');
                        $("#loginform").submit_login(); //SUBMIT FORM 

                        if(data.error)
                        {
                            console.log('data.msg'); 
                        }
                        else
                        {
                            window.location.href = 'myaccount.php';
                        }
                    },
                    error: function(jqXHR, textStatus, errorThrown) 
                    {
                        $("#loginreturn").html('<pre><code class="returndata">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
                    }
                });
                e.preventDefault();  //STOP default action
            });
    });

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

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