Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...