user2669997 user2669997 - 5 months ago 8
Javascript Question

AJAX example not working on page loading

I am trying to learn a little JS AJAX but cannot get my first script to work.

I have a file called test_output.php with the following php script

<?php
echo call_funtion1();

function call_funtion1(){
return 'hello';
}
?>


I open a page on the web called letssee.php with the following script

<html>
<body>
<h1>Ajax</h1>

<script type="text/javascript">

var data_from_test;

$.get('http://www.motorhomesites.org.uk/test_output.php', function(data){
data_from_test = data;
)};

document.write ('test');
document.write (data_from_test);

</script>
</body>
</html>


I thought when opening the letssee.php I would get the output displayed from the test_output.php file. But nothing is happening. It even ignores the test document.write. What am I doing wrong please help

Answer
Use Following code...     

  // Add library
  <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>

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

  var data_from_test;

  $.get('http://www.motorhomesites.org.uk/test_output.php', function(data){
  data_from_test = data;
  document.write ('test');    
  document.write (data_from_test);

});

</script>