doumu1212 2019-07-13 22:09
浏览 73

来自php脚本的数据在javascript错误输出

I have a javascript function called check1() that connects to server side via ajax and returns results. This occurs three times so check1 is called three times. If result is ok i then call function htmlnow1 . If result is not ok i call htmlnow2. First one i use $html=”hello 1” to return the $html code . Second function i use $html=”hello 2” . The first time htmlnow1() is triggered. The second and third time htmlnow2() is triggered. So it should show me logically thinking the first time hello 1 output and the other two times hello 2 output so the output should be hello 1 hello 2 hello 2 . The issue is that on my page i see both hello 2 everytime. So i have output hello 2 hello 2 hello 2 although i check with alert box that only the second and third time is triggered the htmlnow2() function. Any thoughts why this might happening? Any help appreciated!

    <script type="text/javascript">

            function htmlnow1()
            {
                <?php


             $html=" hello 1";

             ?>
            }

            function htmlnow2()
            {
                <?php


             $html=" hello 2";

             ?>
            }

            function check1()
            {

            $.ajax({
            url: "/ok/alert.php",
            type: "post",
            dataType: 'json',
            data: {
            reg: "success",
            checkifexist: 1
            },
            success:function(response)
            {
                var JsonObject=response['checkifexist'];


            if (JsonObject==1)
            {

            alert('text ' + JsonObject);
            htmlnow();



            }
            else if (JsonObject==0)
            {
            alert('text '+JsonObject);
            htmlnow1();
            }

            },
            error: function(x,y,z){
            alert('An error has occurred:
' + x + '
' + y + '
' + z);
            }
            });

            }

            </script>
<script>
window.onload=check1();
</script>
  • 写回答

1条回答 默认 最新

  • doutuobao4004 2019-07-13 22:35
    关注

    On the server side, the HTML page is generated, including the javascript code that is sent to client to be run. Javascript can not create and run php code, it can only send http request to server to run php code there. The php code is not at client side.

    So the server side creates empty functions htmlnow1() and htmlnow2(), sets the $html variable to hello 1 and immediately rewrites it to hello 2. Calling the empty functions has no effect.

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀