0

i am using thie jquery code to poll my database:

jQuery(function($){
  setInterval(function(){
    $.get( 'getrows.php', function(newRowCount){
      $('#rowcounter').html( newRowCount );
    });
  },5000); // 5000ms == 5 seconds
});

and display the results here:

<p>There are <span id='rowcounter'>xx</span> rows in the DB.</p>

but i am not sure what to put in the getrows.php file... does there need to be any html tags with an ID of rowcounter or newRowCount?

4 Answers 4

1

After getting the number of rows in your database, just do:

<?php echo $rowCount; //Or whatever value you want to show ?>

That's all

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

8 Comments

hmm... i have this in getrows.php <?php $pbx01_conn=mysql_connect("94.199.187.7:3306","integra","Yg4tgtYoAeat"); mysql_select_db("voipnow",$pbx01_conn); $sql="SELECT * from extension_rule where id = '1580' "; $rs=mysql_query($sql,$pbx01_conn); $result=mysql_fetch_array($rs); echo $result["status"]; ?> but its just displaying There are xx rows in the DB.
try to execute getrows.php on a different browser page. It's possible your query returned no results.
the path was incorrect :) i am trying this now: if($result["status"] == '1') { echo 'on'; } elseif($result["status"] == '0') { echo 'off'; } but its not returning anything but if i change it to just echo 'hello'; is displays
It seems your Javascript doesn't work as excpected: your "xx" isn't erased. Anyways, concerning your last comment: could you just try a echo $result["status"];?
got it :) working fine now. is there anyway to change it so it doesnt check another page (getrows.php) - can it check some PHP code on the same page?
|
0

Looking at that code, all the PHP file needs to do is emit a number. You'd have normal data-access code like you would anywhere else, then just:

echo $someValue;

Comments

0

jQuery intelligently guesses the type of data returned by your php script (definable in your php request's script with ContentType).

A simple php script would return HTML/Text data, so you just have to "echo" your number in your PHP script. Then your newRowCount JS value will contain your PHP-echoed number.

Comments

0

You're simply requesting getrows.php and expecting a response from it.

You can do anything there, as long as you echo something. Like:

<?php

     echo rand(1,10);

?>

When jQuery requests this page, PHP will generate a random number, and jQuery will append it to HTML.

Basically, you can respond anything there. jQuery doesn't care what you're responding.

Comments

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.