1

I need to update a database table using javascript in my html page , what i need is when loading this page i want to update the date in database every 30 sec

var timer = setInterval(function(){
  updatetime()   // i want to call the php function from java
  },30000);
  
<?php 
  
function updatetime(){
    $query="UPDATE user SET time='......";  // here i update the database
    $result=mysql_query($query);
   }

?>

3
  • 1
    Stop using deprecated mysql_* functions. Use PDO / MySQLi instead Commented Nov 24, 2015 at 9:59
  • i tried to put update time in php like this <script> <?php updatetime() ?> </script> but i only works for 1 time and didnt execute every 30 sec , and i tried to put inside this an alert to show if the interval is running , the results was that the alert is running every 30 sec but the update function is running the first time . Commented Nov 24, 2015 at 10:00
  • And you completely mixed up client-side & server-side scripts. Use AJAX to call server-side scripts. Commented Nov 24, 2015 at 10:01

1 Answer 1

3

you cannot call php function fron script like that. Use ajax for that

script

 var timer = setInterval(function(){
   updatetime()   // i want to call the php function from java
 },30000);

function updatetime(){
    $.ajax({
         url:"filename.php",
         type:"POST",
         successs: function(data) {

         }
    });
 }

Create a file called filename.php and add this code in that function,

<?php
    $query="UPDATE user SET time='......";  // here i update the database
    $result=mysql_query($query);
?>

As mentioned by raptor, stop using mysql_* use mysqli_* or pdo instad.

Sign up to request clarification or add additional context in comments.

3 Comments

10x a lot this solution is perfect it works 100% thanks
Glad i could help you:)
niranjan i need to ask another question , i have a number of html pages and i need to put that script code in all pages , i can include it ? like php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.