Call php file from javascript

Hi, I searched on the Internet, but can't get it to work. I am trying to call the databaseUpdated.php file (placed in the same folder as the index.php file) from a function that is called every 10 seconds.

If I put the following in index.php

<script type='text/javascript'>updateboolean();</script>; 

the function starts, so the problem is not that the php file is not being read.

databaseUpdated.php file

 <body> <?php echo ("<script type='text/javascript'>updateboolean();</script>;"); ?> </body> 

And here are my functions in javascript

  $(document).ready(function(){ setInterval(function() { $.get("databaseUpdated.php");//Can't get this to work any obvious reason for this (And yes I have jquery working)? return false; }, 10000); }); function updateboolean(){ alert("Database updated"); document.location.reload(true); } 

Thanks in advance =)

Edit

_________________________________________________________________________________________

When i do

alerts (data); in the function below i get the result when the image shows

 $(document).ready(function(){ setInterval(function() { $.get('databaseUpdated.php', function(data) { alert('Load was performed.'); alert(data); eval(data); }); }, 5000); }); 

enter image description here

But eval (data) doesn't seem to work

+4
source share
5 answers
 $.get("databaseUpdated.php"); // This will only return contents of that file The scripts in that file are not executed. To execute them you need to do eval(). So Try This $.get('databaseUpdated.php', function(data) { eval(data); }); 

In addition, you may need to modify your php file as follows:

 echo ("updateboolean();"); 
+3
source

where is your ajax callback function so it should be

  $(document).ready(function(){ setInterval(function() { $.get('databaseUpdated.php', function(data) { alert('Load was performed.'); }); }, 10000); }); 
+3
source

Try the following:

 $(document).ready(function(){ setInterval(function() { $.get('databaseUpdated.php', function(data) { alert("Database updated"); // or alert(data); //in case you return data from php document.location.reload(true); }); }, 10000); }); 
+1
source

By executing $.get("databaseUpdated.php") , you request a PHP page, but ignore the return results.

Try the following:

Php

 echo "updateboolean();"; 

JavaScript:

 $.getScript("databaseUpdated.php"); 
-1
source

try this instead of your echo

  echo "<script>updateboolean();</script>"; 
-3
source

Source: https://habr.com/ru/post/1478981/


All Articles