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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况