douyouyi8878
2016-01-02 19:13
浏览 32

在Ajax php和mysql中读取响应

I can't get the code to work and redirect to 2 different pages depending if the information is correct or not...

So far I have this on my login page:

$(function () {
$('#form').on('submit', function (e) {
    e.preventDefault();
    $.ajax({
        type: 'post',
        url: 'newfile.php',
        data: $('#form').serialize(),
        success: function (response){
        alert(response); 
                    if (success.text="Username or Password incorrect")
                        window.location.href = "index.php"; 
                    else if (success.text="login successful") {
                        window.location.href = "login_success.html";
                    } else {  
                    }
            }
        })
})

and the information Im reading from is (from another page):

<?php    
// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
     die(" Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
$sql="SELECT myusername, mypassword FROM user WHERE myusername = '" . mysqli_real_escape_string($conn, $myusername) . "' and mypassword = '" . mysqli_real_escape_string($conn, $mypassword) . "';";
$result = $conn->query($sql);
if ($result->num_rows >0) {
    echo "login successful";
} else {
    echo "Username or Password incorrect";
}
$conn->close();

?> 

图片转代码服务由CSDN问答提供 功能建议

我无法使代码工作并重定向到2个不同的页面,具体取决于信息是否正确。 ..

到目前为止,我的登录页面上有这个:

  $(function(){
 $('#form  ')。on('submit',function(e){
 e.preventDefault(); 
 $ .ajax({
 type:'post',
 url:'newfile.php',
 data  :$('#form')。serialize(),
 success:function(response){
 alert(response); 
 if(success.text =“Username or Password incorrect”)
 window.location。  href =“index.php”; 
 if if(success.text =“login successful”){
 window.location.href =“login_success.html”; 
} else {
} 
} 
  })
})
   
 
 

我读取的信息是(来自另一页):

   &lt;?php 
 //创建连接
 $ conn = new mysqli($ servername  ,$ username,$ password,$ database); 
 
 //检查连接
if($ conn-&gt; connect_error){
 die(“连接失败:”。  $ conn-&gt; connect_error); 
} else {
echo“已成功连接”; 
} 
 $ sql =“SELECT myusername,mypassword FROM user WHERE myusername ='”。  mysqli_real_escape_string($ conn,$ myusername)。  “'和mypassword ='”。  mysqli_real_escape_string($ conn,$ mypassword)。  “';”; 
 $ result = $ conn-&gt; query($ sql); 
if($ result-&gt; num_rows&gt; 0){
 echo“login successful”; 
} else {
  echo“用户名或密码不正确”; 
} 
 $ conn-&gt; close(); 
 
?&gt;  
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duan1227 2016-01-02 19:39
    已采纳

    I hope this will work for you try this:

    if (response=="usernames or Password incorrect") {
        window.location.href = "index.php";
    } 
    else if (response=="login successful") 
    {
        window.location.href = "login_success.html";
     } 
    else { }
    

    Use this code in ajax success. Actually you are using simple ECHO in PHP and using response.text in ajax success.

    UPDATE:

    you are using = sign for comparing it should be == operator for compare.

    UPDATE 2:

    i suggest to use status as true false not long string in php like:

    if ($result->num_rows >0) {
        echo true;
    } else {
        echo false;
    }
    

    Than in ajax response:

    if(response == true){
    // Success url
    }
    else {
    // failure url
    }
    
    点赞 评论
  • dongqijuan3786 2016-01-02 19:23

    The variable success will be undefined inside the success callback function. So the next line will not be executed. So the page will not be redirected. According to your php code , you need to check if response is equal to the corresponding result of not.

    点赞 评论

相关推荐 更多相似问题