如果(在jascript条件失败) [英] if(condition fails in jascript)

查看:97
本文介绍了如果(在jascript条件失败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code这是工作在本地主机中的窗口。但在同一服务器code失败。

这是没有得到执行,即使数据==发现写的如果条件; 我查了回来这是发现,但无法弄清楚,为什么code没有执行正确的数据值

 函数checkAvailability(){
  $(#loaderIcon)显示()。
  jQuery.ajax({
    网址:ajaxcheck.php
    数据:tran_id ='+ $(#tran_id)VAL()
    键入:POST,
    成功:功能(数据){
      的console.log(数据);
      //变种X =数据;
      $(#loaderIcon)隐藏()。
      //console.log((data=="found));
      如果(数据==搜)
      {
        $(#singlebutton)丙(禁用,假);
        的console.log(失败);
        $(#tran_id状态)HTML(发现)。
      }
      其他
      {
        的console.log(数据);
        $(#singlebutton)丙(禁用,真正的);
        $(#tran_id状态)HTML()。
        的console.log(SS);
      }
    },
    错误:功能(){}
  });
}
 

下面是ajaxcheck.php

 < PHP
require_once(dbcontroller.php);
$ db_handle =新DBController();
$空间=​​;

如果(!空($ _ POST [tran_id])){
  $结果= mysql_query(SELECT COUNT(*)FROM银行WHERE tran_id ='$空间$ _ POST [tran_id]。');
  $行= mysql_fetch_row($结果);
  $ USER_COUNT = $行[0];
  如果($ USER_COUNT大于0){
     //回声<跨度类='状态,没有可用的ID = \统计\名称= \统计\价值= \确定\>交易明细计算值:LT; / SPAN>中;
    回声发现;
  }其他{
      //回声<跨度类='的状态可用'ID = \统计\名称= \统计\价值= \不是\>(输入有效的事务ID提交)LT; / SPAN> ;
      回声NOTFOUND;
  }
}
?>
 

解决方案

问题是,你的数据变量是回来了一个新的行字符。有两个解决办法1.修剪返回值。 2.弄清楚为什么PHP是服务于一个新行。

解决方案1:

 如果(data.trim()==搜)
 

这使用JS微调功能,的https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim.

解决方案2:

尝试删除> 从你的PHP文件的末尾(PHP文件仍然有效)。这样,如果有,他们不会充当输出后,它和JS不会接收这些多余的线条。

这本手册:

  

如果一个文件是纯PHP code,它是preferable省略PHP结束标记在文件的结尾。这prevents意外空格或新行PHP结束标记,这可能会导致不良后果,因为PHP将启动输出缓冲时,有没有打算从程序员发送在脚本中的那一点任何输出后添加。

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";
  }
}
?>

解决方案

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.

这篇关于如果(在jascript条件失败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆