douzi2785 2016-11-22 18:16
浏览 33
已采纳

将数据从AJAX发送到PHP

My problem is that I always get the "failed" status, even though I put in the right values while logging on. However, when I modify my $sql in the login.php code into right values from database, I get "success" status. I thought that maybe there is something wrong with sending data from AJAX to PHP. Could you please help me how to figure it out?

This is login.php code:

header('Access-Control-Allow-Origin: *'); 
include "database.php"; 
header("Content-Type: application/json");

$numer=$_POST['number'];
$pass=$_POST['pass'];
$sql="SELECT * FROM `Users` WHERE Phone = '$numer' AND Password = '$pass'";

if ($result=mysqli_query($db,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);

 if($rowcount==1)
 {
  while($row = $result->fetch_assoc()) {
       $dane = array("status" => "success",
    "ID" => $row["ID"],
    "Name"=> $row["Name"],
    "Surname"=> $row["Surname"],
    "Phone"=> $row["Phone"],
    "Password"=> $row["Password"],
    "Admin"=> $row["Admin"],
    "Class"=> $row["Class"],
    "Banned"=> $row["Banned"]
    );

 }
  echo json_encode($dane);
 }
 else
 {
  $dane = array("status"=>"failed");
  echo json_encode($dane);
 }

  mysqli_free_result($result);
  }

mysqli_close($db);

?>

This is the AJAX code:

$("#login").click(function(){
      var number=$("#number").val();
      var pass=$("#password").val();
       if($.trim(number).length>0 & $.trim(pass).length>0)
       {
            $.ajax({
            type: "POST",
            url: "http://smietana.mechanikrac.website.pl/cgi-bin/janeta/login.php",
            data: { number:number, pass:pass}, 
            contentType:"application/json; charset=utf-8", 
            dataType:'json', 
            crossDomain: true,
            cache: false,
            beforeSend: function(){ $("#login").html('Connecting...');
            alert(number+pass);},

            success: function(data){
                alert('test'+data["status"]);

            if(data["status"]=="success")
            {
                localStorage.login="true";
                localStorage.number=number;

                alert(data["status"]);

                window.location.href = "order.html";

                $('#witaj').html(number);

            }

            else if(data["status"]=="failed")
       {
                alert("Błędne Dane");

                $("#login").html('Login again');

                }
                }
      });
    }return false;
   });
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 关于#网络安全#的问题:非对称加密验证
      • ¥20 关于线性代数里施密特正交化和QR分解的疑问
      • ¥15 matlab超类包含解析错误
      • ¥15 python拖拽文件问题
      • ¥15 执行import paddle代码出现错误如何解决?
      • ¥15 hisat2align exited with value 137
      • ¥15 寻找大学生合作开发软件(Delphi)
      • ¥30 AndroidBench&eMMC内存测试速度&Android
      • ¥15 W10 文件共享失败 怎么解决
      • ¥20 b站私信完整导出的方法