简体   繁体   中英

Ajax PHP not returning updated variable

My apologies, similar questions have been asked before - Tried a lot of things and can't figure out why this is not working. I'm calling ajax via a click function. I can't seem to get the jList value to return, updating the variable in the main page.

Here is a simplified version of what is not working. PHP file:

<?php

echo <<<END
  <script type="text/javascript">

    jList = "ELLO!!??";
    //alert(jList);

  </script>
END;

?>

Main Page is this:

<script>
var jList = false;
...
...
...

function loadMore(listFile, nextS, nextE) {
  url = 'files/php/jList.php?l='+ listFile +'&s='+ nextS +'&e='+ nextE;

  $.ajax({
      url: url,
      type: 'POST',
      success:function(results) {
          console.log(jList); // can't get the var to update with the value from PHP 
      }
  });

}
...
...
...
  $("#readMore").unbind("click").click(function(e){
    loadMore(listFile, nextS, nextE);
   });
</script> 

Continually returns (console.logs) the value set initially on the main page. What am I missing? Thanks.

Your PHP should be:

<?php
echo 'jList = "ELLO!!??";';
?>

And the jQuery should be:

$.getScript(url, function() {
    console.log(jList);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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