使用Ajax的php头转发问题

时间:2015-06-14 08:35:15

标签: php jquery ajax

表格: -

  <form  name="form"> 
                    <div class="formfieldContainer">
                        <label> Email :</label>
                                                      <div class="login_wrapper loginContainer">
                                                          <span> </span>
                                                        <input type="email" id="email" required name="user_email" autofocus="autofocus" placeholder="Enter Email Address"/>
                                                    </div>
                                            </div>
                    <div class="formfieldContainer">
                        <label> Password :</label>
                        <input type="password" name="user_password" placeholder="Enter Password"/>
                    </div>

                    <input type="button" name= "submit" value="submit" id="submit_login"/>

                </form>

AJAX : -

$("#submit_login").click(function(){
       var username=$('input[name=user_email]').val();
       var password=$('input[name=user_password]').val();        

         $.ajax({
                type: "POST",                    
                url: "newExam.php", 
                data:{name: username,
                       pwd: password},
                       cache: false,                          
                success: function(dataa) {
                   if(dataa)
                   {
                       console.log(dataa);   
                      if(dataa==0)
                      {  $('form').effect( "shake" ); $('p.error').show(); $("#submit_login").val('Login')
                       alert('nodata');
                       }


                    else if(dataa==1){
                    window.location.href="user.php";
                        }
                     }
                 }
              });// ajax
});

PHP : -

<?php  
include('db.php');
$email_php = $_POST['name'];  
 $pwd_php=$_POST['pwd'];
 $sql = "select name from user where email='$email_php' and  password='$pwd_php'";  
$result = mysqli_query($conn,$sql);  
 $num_rows=  mysqli_num_rows($result);
if($num_rows>0){     
   $_SESSION['login_user']= $email_php;       
   echo '1';
 }
 else{  
echo '0';
  }
  ?>

成功登录后,我需要将页面重定向到 user.php 。但我收到以下错误:

Notice: Undefined index: name in C:\xampp\htdocs\demo\newExam.php on line 3

Notice: Undefined index: pwd in C:\xampp\htdocs\demo\newExam.php on line 4

如何克服它?

2 个答案:

答案 0 :(得分:0)

哟应该从php页面重定向(使用标题)而不是使用window.location.href

答案 1 :(得分:0)

如果您不想使用php,可以使用此方法。

$.extend( {
    redirectPost: function(location, args)
    {
        var form = '';
        $.each( args, function( key, value ) {
            form += '<input type="hidden" name="'+key+'" value="'+value+'">';
        });
        $('<form action="'+location+'" method="POST">'+form+'</form>').submit();
    } });

用法

$.redirectPost("user.php", {'key1': 'data1', 'key2': 'data2'});

信用 - https://gist.github.com/aabril/e6b96379ab0eb151a179