Marius Djerv
Marius Djerv

Reputation: 265

Getting php output from url

Im trying to get a number that im writing out in a .php file and into a .js file.

I have a javascript function that inputs data to mySQL on each click and now I need to get this data out again to display on the page.

Trying to do a simple hour to make HTML5 app, but hour is almost up and im lost.

So my php file looks like this:

<?php 
include 'connect.php';

$tCount = mysql_query ("SELECT SUM(clicks) FROM clicks");
    while($row = mysql_fetch_array($tCount)){ 

    $totalCounts = $row['SUM(clicks)'];   
}

?>

    <span id="foo"><? echo $totalCounts; ?></span>

And as you can see I just get every row from the table and SUM it out. Now I need to display this on the screen but on each click that happends from me or a another user, the totalCounts will update itself.

And I can not just echo this out in the index file, this must be external so need need to know if I can parseINT this out from url or other way.

Thank you in advance

Upvotes: 0

Views: 158

Answers (2)

Oshan Wisumperuma
Oshan Wisumperuma

Reputation: 1958

Question is not clear, though...

php

echo json_encode(array('totalCounts' => $totalCounts));

js using jquery

function set(){
      $.ajax({
                url: "tophp",
                type: "GET",
                success: function(data) {
                    var j = JSON.parse(data);
                    $("foo").text(j.totalCounts);
                }
         });
}

need to call above ajax function from using timer. setInterval(set,1000); cant change rendered html page from server like observer design pattern.

Upvotes: 1

dev
dev

Reputation: 439

$tCount = mysql_query ("SELECT SUM(clicks) as total FROM clicks");
while($row = mysql_fetch_array($tCount)){ 

$totalCounts = $row['total']; }  

try this instead

Upvotes: 0

Related Questions