0

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:\n' + x + '\n' + y + '\n' + z);
            }
            });

            }

            </script>
<script>
window.onload=check1();
</script>
stefanosn
  • 3,264
  • 10
  • 53
  • 79

1 Answers1

0

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.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • k...So how this example should be changed to achieve the correct output? I need to show different html output based on the results of the server side. – stefanosn Jul 13 '19 at 22:40
  • @stefanosn I am not sure what the $html variable does in your php code, but you could just `echo "hello 1"` in the alert.php script. That should pass to the `response` parameter in success function. – Jan Turoň Jul 13 '19 at 22:48
  • hmm And how i am going to show it on my $html code? $html is php and response is javascript. How i am going to pass the one variable tot the other? $html is the code that builds the page... – stefanosn Jul 13 '19 at 22:51
  • I am asking because i have to go server side then get results and show. This is happening three times. – stefanosn Jul 13 '19 at 23:00
  • @stefanosn You can not pass php variable to javascript. You can pass php output to javascript via http request. You can set the `url:` to another php script which echoes any output you want and the output is going to the response parameter by ajax call. Also look at [this answer](https://stackoverflow.com/a/21192061/343721) – Jan Turoň Jul 13 '19 at 23:36