views:

7

answers:

1

I'm trying to retrieve data from a php file named return that contains <?php echo 'here is a string'; ?>. I'm doing this through an html file containing ` div { color:blue; } span { color:red; }

var x; $.get("return.php", function(data){ x = data; }) function showAlert() {alert(x);} $(document).ready(function(){ alert(x); });

` When the button is clicked it retrieves and displays the code fine, but on the page loading, it displays "undefined" instead of the data in return.php. Any solutions?

A: 

On Page loading it says undefined coz when the page load the value of x has not yet been populated. When the page loads only after than the Ajax call goes to your php file and then populates your variable x. So if you r trying to get value of x on document.ready it wont give you.

sushil bharwani