0

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.

3
  • The return data from AJAX isn't executed, it's just put in the results variable. Commented Feb 22, 2014 at 2:35
  • If you want it to be executed, use $.getScript, and don't put the <script> tags in the return data. Commented Feb 22, 2014 at 2:36
  • @Barmar thanks for the info - could you show me what that looks like in an answer - it would save me another hour of trying to figure that part out. Commented Feb 22, 2014 at 2:40

1 Answer 1

1

Your PHP should be:

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

And the jQuery should be:

$.getScript(url, function() {
    console.log(jList);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.