douchuanchai2793 2019-05-28 16:30
浏览 48

来自jQuery回调的数据无法比较

I'm trying to make a "sign in status" thing.

Here is a summary of what is happening.

User fills our field and jQuery request is sent. Credentials are validated. Screen displays a welcome message.

So I can get the welcome message sent back to me if the credentials are valid (or error if credentials are false), but here is where the issue resides...

I am having a really difficult time storing anything in PHP as a global variable using my only jQuery (no included file) approach... So my workaround was to take the passed message (Let's just say when credentials are valid, I pass back something like "X" or "1"), and then when the data comes back in the jQuery, I put an if statement in the callback, but it isn't working.

I know that the data being passed is matching what is being compared, and i've tested many different things to pass back, but the comparison is not being done.

Perhaps it isn't possible to do things like if statements in a jQuery callback, but also maybe I'm doing something wrong.

HTML:

<label>Sign In</label>
<br>
<label>Username</label>
<input type="text" id="name1"> 
 <label>Password</label>
<input type="text" id="pass1"> 
<br>
<button type="submit" id="button2">Sign In</button> 
<div id = "xx1">Status: Offline</div>
   <div id = "xx2"></div>

jQuery:

$(document).ready(function(){
        $("#button2").click(function(){
            var name1=$("#name1").val();
            var pass1=$("#pass1").val();
            var key = "signIn";
            $.ajax({
                url:'rpc.php',
                method:'POST',
                data:{
                    name1:name1,
                    pass1:pass1,
                    key:key

                },
               success:function(data){
                   if(data === '1')
                   {
                      document.getElementById('xx1').innerHTML = "Status: Online";
                   }
                   document.getElementById('xx2').innerHTML = data;
                   //var p = data;


               }
            });
        });
    });

(xx2 is updating by the way)

Lastly, relevant bits of my rpc.php:

else if($_POST['key'] === "signIn")
{
$name1=$_POST['name1'];
$pass1 = $_POST['pass1'];

if($name1 !== "" && $pass1 !== "")
{


    $sql = "SELECT * FROM whatever";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    if($name1 === $row["username"])
    {
        $UNTrue = true;
        if (password_verify($pass1, $row['password'])) {
            $PassTrue = true;
        }

    }


}
} else {
//echo "0 results";
}




if($UNTrue === true && $PassTrue === true)
{
    echo "1";


$conn->close();
}
else
{

    echo "<p  align=center style = 'color:#ba261b'>(Incorrect Username or Password) </p>";

    $conn->close();
}

}
else
{

   echo "<p  align=center style = 'color:#ba261b'>(Please Fill Required Fields) </p>";

$conn->close();
}
}

So data is "1" in this scenario, as displayed to my via xx2, and xx1 remains as "Status: Offline".

I'm wondering if I have to store the data in a JavaScript variable first, and then later somehow referencing it again ASAP.

The other option would be to figure out how to use PHP global variables without file inclusion.

  • 写回答

1条回答 默认 最新

  • duansang8388 2019-05-28 16:36
    关注

    Is it possible even though you're echoing "1", it's getting interpreted as a number? Assuming the data response you get in the success callback is literally just what your PHP script echoes, that's the first thing that jumps out to me, since (1 === "1") is false.

    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗