简体   繁体   English

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

[英]data from php script in javascript wrong output

I have a javascript function called check1() that connects to server side via ajax and returns results. 我有一个名为check1()的javascript函数,该函数通过ajax连接到服务器端并返回结果。 This occurs three times so check1 is called three times. 这会发生3次,因此check1被调用了3次。 If result is ok i then call function htmlnow1 . 如果结果正常,则调用函数htmlnow1。 If result is not ok i call htmlnow2. 如果结果不正确,请致电htmlnow2。 First one i use $html=”hello 1” to return the $html code . 第一个我使用$ html =“ hello 1”返回$ html代码。 Second function i use $html=”hello 2” . 第二个功能我使用$ html =“ hello 2”。 The first time htmlnow1() is triggered. 第一次触发htmlnow1() The second and third time htmlnow2() is triggered. 第二次和第三次htmlnow2()被触发。 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 . 因此,它应该向我展示逻辑上的思考,第一次是hello 1输出,另外两次是hello 2输出,因此输出应该是hello 1 hello 2 hello 2。 The issue is that on my page i see both hello 2 everytime. 问题是在我的页面上我每次都看到两个hello 2。 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. 所以我已经输出了hello 2 hello 2 hello 2,尽管我检查了警告框,仅第二次和第三次触发了htmlnow2()函数。 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:\n' + x + '\n' + y + '\n' + z);
            }
            });

            }

            </script>
<script>
window.onload=check1();
</script>

On the server side, the HTML page is generated, including the javascript code that is sent to client to be run. 在服务器端,将生成HTML页面,包括发送到客户端以运行的javascript代码。 Javascript can not create and run php code, it can only send http request to server to run php code there. Javascript无法创建和运行php代码,它只能向服务器发送http请求以在其中运行php代码。 The php code is not at client side. php代码不在客户端。

So the server side creates empty functions htmlnow1() and htmlnow2() , sets the $html variable to hello 1 and immediately rewrites it to hello 2 . 因此,服务器端将创建空函数htmlnow1()htmlnow2() ,将$html变量设置为hello 1然后立即将其重写为hello 2 Calling the empty functions has no effect. 调用空函数无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM