-1

I am having some trouble communicating with a PHP file via AJAX. When I execute the on click event below I expect it to give me an alert of "hello world" from the PHP file. Instead it only gives me the alert of "yep" from the AJAX success. Here is my AJAX call:

jQuery(".clickme").click(function (){

//start ajax call 

jQuery(function test(){ 
jQuery.ajax ({
type: 'POST',
url: "test.php",
 success: function(data) { 
alert("yep");
}
});
});

//end ajax call
});

and in my test.php file there is just

<?php
echo '<script language="javascript">';
echo 'alert("Hello world!")';
echo '</script>';
?>

I have no idea why its not echoing my alert

red house 87
  • 1,837
  • 9
  • 50
  • 99
  • 1
    do `alert(data);` and you will find out. – Federkun Nov 23 '15 at 23:40
  • You have to evaluate response data, currently you only have it in the data variable during the callback, but you are doing nothing with it. – k0pernikus Nov 23 '15 at 23:41
  • You should really go and read some more about how javascript works. [Here's an SO Q/A about it](http://stackoverflow.com/questions/1510011/how-does-ajax-work) –  Nov 24 '15 at 00:48
  • Im trying to and I got stuck hence why I asked here... Not everything is apparent in manuals when you are learning – red house 87 Nov 24 '15 at 02:49

1 Answers1

0

Your ajax call success indeed, that's why it alerts "yep", but didn't tell your success function what to do with the data. Maybe if you do this:

success: function(data) { 
  document.write(data);
}
Eduardo Escobar
  • 3,301
  • 2
  • 18
  • 15