-1

I have read the other similar questions, but not found the answer, so here it is:

<?php     
function load($page) {     
    echo "page: ".$page;
}

echo "
<script type='text/javascript'>
  var page = 0;
  window.addEventListener(...., function(){
    var x = .....
    ......
    if(x = ....)
    {
      page = page + 1;
      var runQuery = '<?php load(page); ?>'
    }
  })
</script>  
";    
?>

The problem is that <?php load(page); ?> is not executed. If I write load(page); outside the echo, it works. Can anyone help me ?

user7637745
  • 965
  • 2
  • 14
  • 27
zepifiwe
  • 9
  • 3

1 Answers1

2

Change the function to return:

function load($page) { 

    return "page: ".$page;
}

You're executing PHP with the echo so just use the return of load():

echo "
            <script type='text/javascript'>
            var page = 0;
            window.addEventListener(...., function(){
                var x = .....
                ......
                if(x = ....)
                {
                    page = page + 1;
                    var runQuery = '" . load($page) . "'
                }
            })
            </script>  
";
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • thank for the fast reply but still does not work. Every time window.addEventListener() is called, i would like to pass the value page to the load function. – zepifiwe Jul 02 '18 at 18:05