Solution 1 :

You have a few issues with your code i have tried to point them all out below for you:

function updateCDNonts() {
   $.get("x.x.x.x:8080/getliveontscdn.php", function(result){ //removed equals from url
   $("#countonts").html(result); //removed curly brace that shouldn't be there
 }); //added missing bracket and semicolon

Also as someone else noted watchout for cors if your url is different.

Solution 2 :

I managed to get it working. I changed my php script to return a json, here is the json: {"cdnonts":"144","eagonts":"0","stamonts":null,"foxonts":null,"pentonts":null,"topponts":null,"wickhamonts":null}

And in the html I did the following

            <script language="javascript">

                window.onload = function() {
                $.ajax({ 
                type: 'GET', 
                url: 'countallonts.php',
                dataType: "json",
                data: { get_param: 'cdnonts' },
                success: function(data){
                     $('span#cdnonts').html( data.cdnonts);
                     $('span#eagonts').html( data.eagonts);
                }});}

            </script>   

Although I am not sure why it works, because I expected data variable to return just the “cdnonts” object, but I suspect it parses all of the objects instead?

Thank you for you help.

Problem :

Hello again stack overflow

I have a simple PHP file (countsomething.php) that looks up a number and echo’s it.

How do I get ajax to update a simple span element on my HTML page.

I’ve tried triggering the ajax on page load with : <body class="skin-blue" onload="updateCDNonts();">

The JS

function updateCDNonts() {
  $.get("x.x.x.x:8080/getliveontscdn.php=", function(result){
    $("#countonts").html(result)};
}

The HTML
<span id="countonts" class="info-box-number">0</span>

Can someone point me in the right direction ?

Comments

Comment posted by Barmar

You’re missing a

Comment posted by Barmar

You also have an extra

Comment posted by Barmar

If

Comment posted by Barmar

Are you getting errors in the JavaScript console?

Comment posted by davidc2p

Console.log your js result variable to check where the problem comes from.

By

Leave a Reply

Your email address will not be published. Required fields are marked *