简体   繁体   中英

header doesn't redirect to another page ajax php

I'm working on login form request using ajax concept. Below i've posted my codes related to login. It doesn't redirect to myaccount.php page. if i use this echo'<script>window.location="myaccount.php";</script>'; javascript function it will work properly. But, here i want to do with php. it returns the myaccount.php results as a ajax message.

错误图片

I'm getting the error message using this <div id="loginreturn"></div> . Above I've posted an error image. myaccount.php page results also shows inside the <div id="loginreturn"></div> . That is my problem. it doesn't redirect to another php page. How to solve this error?

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

<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'

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'

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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