问题是这样的:
  1. 我通过浏览器或命令行执行.php文件,操作数据库没问题。
  2. 我通过ajax,后台使用的php文件,如果不操作数据库,直接 echo 数据,网页也工作正常。
  
问题就出在:在2的步骤上,把echo返回数据的地方,用1中测试过的代码替换在此,echo出来,但网页就sb了,not work了。搞了好久了,请哪位出手帮助解决一下?不胜感激!

解决方案 »

  1.   

    这取决于 php 的输出是否符合 js 的要求
      

  2.   


    版主老大,您好,3的输出和1前面测试是一样的。我把3中改为连接数据库等几条简单语句,然后判断成功,直接echo和1测试一样的返回。也是不行。只有一行行把连接数据库的语句注释才行。也就是说这里不能操作数据库。或者我整理一个简单的演示,您帮我看下?
      

  3.   

    谢谢版主,我把相关html,php, js进行简化,做一个测试的,却发现这个简化的可以工作。
    而原来的还是不行。
    我怀疑是不是哪里配置之类,或者是缓存之类?
      

  4.   

    发现是定义一个CSS样式,不定义时是正常显示,定义时就是带数据库操作时不正常。
    太奇怪了。
      

  5.   


    感谢版主回复。
    前段时间有事忙别的去了。代码传上来,麻烦版主帮忙看一下了。1. html文件<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>A集团</title>
    <style type="text/css">
    <!--
    body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; }
    ul li{padding:0px;margin:0px; list-style-type:none;}
    #top1{ background:#541B60; padding: 15px 0px 0px 0px ;  text-align:center; font-family:"微软雅黑"; font-size:24px; height:30px; line-height:30px; color:#FFFFFF; letter-spacing:6px;}
    #top2{ background:#541B60; padding: 0px 0px 15px 0px ;  text-align:center; font-family:"微软雅黑"; font-size:24px; height:30px; line-height:30px; color:#FFFFFF; letter-spacing:3px;}
    #hquan{ display:inline-block; color:#ffffff; background:#541B60;  height:50px;  line-height:50px; font-family:"微软雅黑"; font-size:32px; text-align:center;-webkit-border-radius:50px; -moz-border-radius:50px; border-radius:50px; padding:5px 10px 5px 10px;}
    #xquanju{text-align:center; padding:10px 0px 10px 0px;}#mob{ display:inline-block; color:#541B60; width:240px; height:45px;  line-height:45px; font-family:"微软雅黑"; font-size:28px; text-align:center;-webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; padding:5px 10px 5px 10px; margin:3px; border:solid 1px #541B60;}#anniu{ display:inline-block; color:#ffffff; width:240px; background-color:#541B60; height:60px;  line-height:35px; font-family:"微软雅黑"; font-size:20px; text-align:center;-webkit-border-radius:30px; -moz-border-radius:30px; border-radius:30px; padding:5px 10px 5px 10px; margin:3px; border:solid 1px #541B60;}#ziti1{ font-family:"微软雅黑"; font-size:24px; height:40px; line-height:40px; color:#333333; text-align:center;padding:10px 0px 0px 0px; font-weight:bold; letter-spacing:2px;}
    #ziti2{ font-family:"微软雅黑"; font-size:24px; height:40px; line-height:40px; color:#333333; text-align:center;padding:0px 0px 10px 0px; font-weight:bold;  letter-spacing:2px;}
    -->
    </style>
    <script>
       window.onload=function()
       {
        var oTxt1=document.getElementById('mob');
        oTxt1.onfocus=function()
        {
        this.value='';
       
        }
        oTxt1.onblur=function()
        {
        if(this.value=='')
        {
        this.value='输入手机号码';
        }
        }
       }
    </script>
    </head>
    <script type="text/javascript" src="js/Show/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="js/Show/checkQuery.js"></script>
    <body><div id="top1">A子集团V1.0</div>
    <div id="top2">查询管理系统</div>
    <h2 style="font-family:'微软雅黑'; color:#000;display:none;text-align:center;" id="showError">&nbsp; </h2>
    <div id="ziti1">队列</div>
    <div id="ziti2">总数是:</div>
    <div id="xquanju">
    <div id="hquan">XX</div>
    </div>
    <form action="" method="get">
    <div id="xquanju">
      <label>
      <input name="textfield" type="text" id="mob" value="输入手机号码" />
      </label>
    </div>
    <div id="xquanju">
      <input name="anniu" type="button" id="anniu" value="排队信息查询" />
    </div>
    </form></body>
    </html>
      

  6.   

    2.  checkQuery.js文件
     
    $(document).ready(function () {
    loadInit();
    loadEvevnt();
     
    });
    loadInit = function () {  
    $.ajax({
    url: "./ajax.php?tag=LoadInfoTag",
    type: "POST",
    success: function (msg) {
    var tt = msg.split("^");
    if (tt[0] == 'true') {
    document.getElementById('hquan').innerHTML = tt[2];
    } else {
    }
    },
    error: function (cc) {
    debugger;
    }
    });
    }
    loadEvevnt = function() {
        $('#anniu').bind('click', function() {
            $.ajax({
                url: "./ajax.php?tag=queryInfoTag",
                type: "POST",
                data: {
                    m: document.getElementById("mob").value
                },
                success: function(msg) {
                    var tt = msg.split("^");
                    if (tt[0] == 'true') {
                        $('#showError').css('display', 'none');
                    } else if (tt[1] == 'cf') {
                        $('#showError').html("手机号有重复!");
                        $('#showError').css('display', '');
                    }
                    else {
                        $('#showError').html("手机号不存在!");
                        $('#showError').css('display', '');
                    }
                },
                error: function(cc) {
                    debugger;
                }
            });
        });
    }
      

  7.   

    3. ajax.php<?php
    header("Content-type: text/html; charset=gb2312");
    header("Cache-Control: no-cache, must-revalidate");$p_tag = $_GET["tag"];
    if ($p_tag == "LoadInfoTag")
    {
        ///echo "true^1^332";
        include("./conn/conn.php");
        $dbconn=mysql_connect($sqlservername,$sqlsrvloginname,$sqlsrvloginpws);
        mysql_select_db($sqldbname,$dbconn);
        mysql_query("set names 'gb2312' ");    $sqlstr = "select count(f_id),  ifnull(max(f_qid),0)  from t_users";
        $sql=mysql_query($sqlstr,$dbconn);
        $result=mysql_fetch_array($sql);
        if(!$result)
        {
                echo "false^0^0";
        }
        else
        {
                echo $result[1]."^".$result[0];///"true^1^332";
        }
    }
    elseif  ($p_tag == "queryInfoTag")
    {// true/false ^ 
        $p_mob = $_POST["m"];
        include("./conn/conn.php");
        $dbconn=mysql_connect($sqlservername,$sqlsrvloginname,$sqlsrvloginpws);
        mysql_select_db($sqldbname,$dbconn);
        mysql_query("set names 'gb2312' ");    $sqlstr = "select count(*)  from t_users where f_tel='".$p_mob."'";
        $sql=mysql_query($sqlstr,$dbconn);
        $result=mysql_fetch_array($sql);
        if(!$result)
        {
                echo "none.";
        }
        else
        {
                echo "true^".$result[0];
        }
    }
    else
    {        
            echo "true^1^122";
    }
    ?>
      

  8.   

            success: function (msg) {
                alert(msg);
                var tt = msg.split("^");
      

  9.   

    版主,您好,如果把ajax中的关于
    ///echo "true^1^332";
    的注释去掉,就会返回,显示332.但现在这样,感觉就是数据库的操作执行不下去。但我把ajax.php的内容提取出来为test.php,单独通过命令行执行 php test.php,又能执行。所以,感觉好无奈,不知问题到底是怎么了。
      

  10.   

    ajax 程序要通过表单进行调试
    <form action='./ajax.php?tag=queryInfoTag' method=post>
    <input type=text name=m id=mob>
    <input type=submit value=提交>
    </form>
      

  11.   

    好的,版本,但是启动时需要从数据库读取数据,即 LoadInfoTag,那里也不返回啊
      

  12.   

    好的,版本,但是启动时需要从数据库读取数据,即 LoadInfoTag,那里也不返回啊不好意思,版主,打错了个字
      

  13.   

    如果是php连接数据库出问题,那就在php分部调试看连接哪一步出了问题,例如:$dbconn=mysql_connect($sqlservername,$sqlsrvloginname,$sqlsrvloginpws) or exit("connect failed");
                    mysql_select_db($sqldbname,$dbconn) or exit(" select failed");
                    mysql_query("set names 'gb2312' ") or exit("charset failed");
     
        $sqlstr = "select count(f_id),  ifnull(max(f_qid),0)  from t_users";
        $sql=mysql_query($sqlstr,$dbconn) or exit("query failed");分步测试,模块化测试更加容易找到问题的根源。
    另外就是报错的具体形式,错误码之类的最好也贴出来,不然诊断起来很费力。
      

  14.   

    谢谢版主和瓜皮猫尼玛的回复,我又装了个系统,这次用的是php5.6,以前的好象是5.2.
    再把相应的改了,现在好象是可以了!我现在做进一步的开发。另外还想请教版主和瓜皮猫尼玛的是:
    我在ajax.php的queryInfoTag处理中,收到了mob, 然后我再处理后,直接转到另一个a.html页面,
    此a.html需要ajax2.php来刷新信息,则ajax2.php想取得ajax.php中取得的mob,一般是通过session吧?不知有什么简便方式吗?谢谢!
      

  15.   


    再请问下版主,mysqli_query的返回值怎么老是判断不准?就一个select返回值谢谢!
      

  16.   

    SESSION才是简便方法把。其他方法是不是更复杂了。