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 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路