0

In my below code, I am trying to pass the global value of my PHP variable "z" in the ajax_processor.php file.

However Its not working as it doesn't picking up the value from my php code in the body(Code shown at the last)

Could someone please help me on this. I am trying this code to implement a facebook type auto load content on scroll event.

  <head>
  <script type="text/javascript">
      $(document).ready(function(){
        $.ajaxSetup({cache: false}); // disabling cache, omit if u dont need
        var defaultBtnText = "<span class='pseudolink'>Load More    Content</span>";
        var buttonLoadingText = "<img src='images/loader.gif' alt='' />   Loading..";
        $(document).scroll(function(){
            if ($(window).scrollTop() + $(window).height() >=   $(document).height())
            {

                loadMore();
            }
        });

        $("#loadButton").click(function(){
        loadMore();

        });

          function loadMore()
          {    alert ('<?php echo $z; ?>');
            $("#loadButton").html(buttonLoadingText);
            $.ajax({
                url: 'ajax_processor.php?global1=<?php echo $z; ?>',
                method: 'get',
                success: function(data){


                    $("#tab1_content1").append(data);

                    $("#loadButton").html(defaultBtnText);
                }
            });
          }

       });
     </script>
    </head>

    Below is the part of my asked code in <body>


    <body>
 <?php
 $query=("SELECT * FROM tbl_content");
 $result=mysql_query($query);
 while ($row = mysql_fetch_array($result)) 
               {
                 $window = $row['id']; 
                 $window = $row['title'];
                  $window = $row['description'];
                 echo "<h2>".$row['id']." ".$row['title']."</h2>";
                 echo "<p>".$row['description']."</p>";
                 $GLOBALS['z']=$row['id'];
                  echo "<h2>".$z."</h2>";
                }

?>
 </body>
Deva
  • 91
  • 6
  • You do realise that PHP runs on the server, build a web page, send it to the browser.... Where javascript can then run. They are not all running together anywhere – RiggsFolly Feb 04 '16 at 14:58
  • Where do you define the `$z` variable? Does the alert message pop up? – Poul Kruijt Feb 04 '16 at 14:58
  • Not enough _Dev_ and maybe too much _ashish_ – RiggsFolly Feb 04 '16 at 15:00
  • @RiggsFolly is right setting as $GLOBAL will not change anything..If you think it does.. – ern Feb 04 '16 at 15:00
  • 1
    @ern is right also becasue the `GLOBALS['z']` is being set after the javascript code has been sent to the output buffer anyway. and that is completely ignoring the fact that `GLOBALS['z']` is being set multiple times as its inside a loop anyway – RiggsFolly Feb 04 '16 at 15:02

1 Answers1

-1

You can take your veriable '$z' before any ajax request in the function like this. Now you can check 'leadMe' for it is really filled by your $z.

$("#loadButton").click(function(){
    loadMore("<?php echo $z; ?>");

    });

      function loadMore(loadMe)
      {    

        $("#loadButton").html(buttonLoadingText);
        $.ajax({
            url: 'ajax_processor.php?global1='+loadMe,
            method: 'get',
            success: function(data){


                $("#tab1_content1").append(data);

                $("#loadButton").html(defaultBtnText);
            }
        });
      }
Mert Öksüz
  • 1,071
  • 6
  • 17
  • Why should the OP "try this"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Feb 04 '16 at 15:03
  • And that wont work with the existing PHP script anyway – RiggsFolly Feb 04 '16 at 15:05
  • @RiggsFolly So you meant to say I cannot acheive it in this script ? – Deva Feb 04 '16 at 15:06
  • You are right i explained what i thinked. Thank you. – Mert Öksüz Feb 04 '16 at 15:07
  • 1
    Devashish: Your script needs a lot of work. Start by understanding how PHP works and then javascript. But mainly that one runs on the server and the other(javascript) only actually runs on the users browser after PHP is long since finished its work – RiggsFolly Feb 04 '16 at 15:08
  • Yes i know, i did not talked about something like that. It is just check only. – Mert Öksüz Feb 04 '16 at 15:08
  • That comment was aimed at the Questioner – RiggsFolly Feb 04 '16 at 15:09
  • @RiggsFolly : I need to understand what I am doing wrong here..I have assigned global variable z which could be used anywhere in this page – Deva Feb 04 '16 at 15:15
  • Yes, but NOT before you create it. The javascript section is before you even create `$GLOBALS['z']` PLUS you change its value in a loop. Really nothing hangs together at all – RiggsFolly Feb 04 '16 at 15:19
  • @RiggsFolly : But here the javascript section is getting called when the scroll even occured before which the variable is already defined. – Deva Feb 04 '16 at 15:22