jquery noob problem with variables (scope?)

Posted by aharon on Stack Overflow See other posts from Stack Overflow or by aharon
Published on 2010-06-11T03:13:48Z Indexed on 2010/06/11 3:22 UTC
Read the original article Hit count: 302

I'm trying to retrieve data from a php file named return that just contains

<?php
echo 'here is a string';
?> 

I'm doing this through an html file containing

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script> 
  var x;
  $.get("return.php", function(data){
   x = data;
 }) 
 function showAlert() {alert(x);}

  $(document).ready(function(){
   alert(x);
 });
  </script>
</head>
<body>
  <input type = "button" value = "Click here" onClick="showAlert();">
</body>

</html>

When the button is clicked it retrieves and displays the code fine, but on the $(document).ready thing, it displays "undefined" instead of the data in return.php. Any solutions?

Thanks.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery