Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

javascript - if(condition fails in jascript)

I have following code which is working in localhost in windows. But in the server same code fails.

It's the if condition which is not getting executed write even if data==found; I checked the returned data value which is found but cant figure out why the code is not executing properly

function checkAvailability() {
  $("#loaderIcon").show();
  jQuery.ajax({
    url: "ajaxcheck.php",
    data:'tran_id='+$("#tran_id").val(),
    type: "POST",
    success:function(data){
      console.log(data);
      //var x=data;
      $("#loaderIcon").hide();
      //console.log((data=="found"));
      if(data=="found")
      {
        $("#singlebutton").prop('disabled', false);
        console.log("fail");
        $("#tran_id-status").html("Found");
      }
      else
      {
        console.log(data);
        $("#singlebutton").prop('disabled', true);
        $("#tran_id-status").html("");  
        console.log("ss");
      }
    },
    error:function (){}
  });
}

Here is ajaxcheck.php

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$space=" ";

if(!empty($_POST["tran_id"])) {
  $result = mysql_query("SELECT count(*) FROM bank WHERE tran_id ='" . $space.$_POST["tran_id"] . "'");
  $row = mysql_fetch_row($result);
  $user_count = $row[0];
  if($user_count>0) {
     // echo "<span class='status-not-available' id="stat"name="stat" value="ok"> Transaction Details Found.</span>";
    echo"found";
  }else{
      //echo "<span class='status-available' id = "stat" name ="stat"value="not"> (Enter Valid transaction id to submit)</span>";
      echo"notfound";
  }
}
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue is that your data variable is coming back with a new line character. There are two solutions to this 1. trim the returned value. 2. figure out why the php is serving a new line.

Solution 1:

if(data.trim()=="found")

This uses the JS trim function, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim.

Solution 2:

Try removing the ?> from the end of your PHP file (the PHP file will still be valid). This way if there are extra lines after it they won't be served as output and the JS wont receive them.

From the manual:

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...