dongyan4157 2014-11-17 17:48
浏览 17
已采纳

如果Else Condition with Ajax responseText from PHP

Im building simple login system using Ajax XMLhttpRequest. PHP File and Javascript all working fine.. but when i use response test in IF Else Condition its not working as i expect.

Here My HTML

<div class="login-form">

        <input type="username" name="username" id="username" class="text-input--underbar" placeholder="Username" value="">
        <input type="password" name="password" id="password" class="text-input--underbar" placeholder="Password" value="">
        <br><br>
        <ons-button modifier="large" onClick="javascript:ajax_post();" class="login-button">Log In</ons-button>
        <br><br>
        <ons-button modifier="quiet" class="forgot-password">Forgot password?</ons-button>
      </div>
<div id="status"></div>

Here my Javascript Code.

function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "http://boost.meximas.com/mobile/login.php";
    var fn = document.getElementById("username").value;
    var ln = document.getElementById("password").value;
    var vars = "username="+fn+"&password="+ln;
    hr.open("POST", url, true);

    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object

    hr.onreadystatechange = function() {
      if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
      document.getElementById("status").innerHTML = return_data;

      if(return_data=="true"){
        alert("Yes Login True");
      }else{
        alert("No Login False");
      }

      }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}

Here my PHP Code

if(isset($_POST['username']) && isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
}

$sql = "SELECT * FROM member WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($con,$sql);


if(!$result){
    echo "failed";
}else{
    echo "true";
}

For the Wrong input also im getting Yes Login True and status inner HTML True that mean PHP file always returning True. but when i check php file alone it works fine.. there is no errors.

i meant using this.

while($row = mysqli_fetch_array($result)) {

    echo "Hello" .$row['email']. "Thanks";
}

it gives correct output.

Im sorry if its unclear.. please let me know.

  • 写回答

1条回答 默认 最新

  • dtest84004 2014-11-17 19:10
    关注

    The function mysqli_query() will only return FALSE if your query produces an error in the database. Otherwise, it will always return TRUE. Giving that, you should check your results in another way, like using mysqli_num_rows().

    Besides, it is very advisable that you sanitize user's inputs before running queries with them. Always remember Booby Tables(and how to prevent it in PHP).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器