0

On my php site, I want to retrieve data every three seconds from a mysql database using javascript.

Problem: when I retrieve data using SELECT * from msgtable, then neither php nor javascript startTime seems to work.

JavaScript:

setInterval(function() {
   var link = document.getElementById("chg");
   link.href = "http://google.com.pk";
   link.innerHTML = "<?php  dynamic(); ?>"; 
}, 3000);

function startTime() {
   var today = new Date();
   var s = today.getSeconds();
   s = checkTime(s);

   if( s == s+3 ) { alert("faraz"); }
   document.getElementById('time').innerHTML= s;
   t = setTimeout( function() { startTime() }, 500 );
}

function changeURL() {
   var link = document.getElementById("chg");
   link.href = "http://google.com.pk";
   link.innerHTML = "Google Pakistan"; 
}

function checkTime( i ) {
   if ( i < 10 ) {
      i = "0" + i;
   }
   return i;
}

php:

<?php 
    $connection = mysql_connect("localhost","root","");
    $db_select = mysql_select_db("msgs",$connection);  
    $result = mysql_query("SELECT * FROM msgtable", $connection);

    function dynamic() {
        echo "faraz";

        while ( $row = mysql_fetch_array( $result ) ) {
        echo $row['msgBody'] ;
        }   
    }
?>

HTML:

<body onLoad="startTime()">
    <div id="chg1"> 3 Seconds to Google Pakistan  </div>
    <a href="http://google.it" id="chg">Google Italia</a>
    <!-- Hafiz Faraz Mukhtar-->
    <div id="time"> Time </div>
    <div class="publicOut">Faraz</div>
</body>
Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
hfarazm
  • 1,407
  • 17
  • 22

2 Answers2

1

You can't call a PHP function through JavaScript like this:

link.innerHTML = "<?php  dynamic(); ?>"; 

You will need to make an AJAX call to run the PHP script and return the result. I would recommend using jQuery and $.ajax, which makes this very easy.

http://api.jquery.com/jQuery.ajax/

Seth
  • 1,353
  • 1
  • 8
  • 17
  • hhmm alrite. then why does it correctly echos "faraz"; in dynamic()? if we remove select and fetch array . – hfarazm Jul 01 '13 at 04:38
  • We can use link.innerHTML = ""; like this in javascript. i am sure. – hfarazm Jul 01 '13 at 04:40
  • It may echo 'faraz', but the JavaScript you posted above is not what's calling it. If you could post the whole code as well as the filenames, it will be easier to see what the problem is. – Seth Jul 01 '13 at 04:40
  • 1
    If you have that JavaScript inside a PHP script, then yes, you can write the syntax like that. When the page is parsed, the PHP tags will be removed before being sent to the browser. – Seth Jul 01 '13 at 04:42
  • this is the whole code sir. ok let me try it jQuery way. Thanks – hfarazm Jul 01 '13 at 05:01
  • No problem. Also, if you get the Firefox browser and also install the firebug addon, this will help you a whole lot because it allows you to monitor the AJAX requests and see the response. – Seth Jul 01 '13 at 05:04
1

You need to use normal ajax or jquery ajax for this .Use javascript setInterval() function for setting an interval

Here is a sample jquery ajax method

        function request()
         {
           $.ajax ({
           url : "";
           data : {},
           dataType : "" ,
           success : function(success) {} ,
           error : function() {}
           });
         }

setInterval() Syntax

     setInterval(request,3000);  // in milliseconds
Jijo John
  • 1,375
  • 9
  • 21