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="">
        <ons-button modifier="large" onClick="javascript:ajax_post();" class="login-button">Log In</ons-button>
        <ons-button modifier="quiet" class="forgot-password">Forgot password?</ons-button>
<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 = "";
    var fn = document.getElementById("username").value;
    var ln = document.getElementById("password").value;
    var vars = "username="+fn+"&password="+ln;"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;

        alert("Yes Login True");
        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);

    echo "failed";
    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).

    点赞 打赏 评论

相关推荐 更多相似问题