SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast readyman's Avatar
    Join Date
    Sep 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow auto-update count, and display, from a mysql database

    This is probably something really easy to do, but I just can't seem to find anything after searching the web for hours. I am assuming that ajax would be the language of choice for this, but I am not totally sure!

    What I am trying to do:

    Pull the count of rows from one of my mysql tables and then automatically display the updated count every few seconds.

    I have read this tutorial, but I just cannot seem to get it to work. Does anyone have a simple solution for this?

    Thanks so much to anyone who can help me!

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you paste your code? You most likely need to send a POST, not GET request. Oh and, repeat after me.. AJAX IS NOT A LANGUAGE.

  3. #3
    SitePoint Enthusiast readyman's Avatar
    Join Date
    Sep 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AJAX IS NOT A LANGUAGE... AJAX IS NOT A LANGUAGE... ok got it (sorry for the sarcasm I just had to do it )

    Code:
    <script language="javascript" type="text/javascript">
        function createRequestObject() {
        
            var req;
        
            if(window.XMLHttpRequest){
                // Firefox, Safari, Opera...
                req = new XMLHttpRequest();
            } else if(window.ActiveXObject) {
                // Internet Explorer 5+
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                // There is an error creating the object,
                // just as an old browser is being used.
                alert('There was a problem creating the XMLHttpRequest object');
            }
        
            return req;
        
        }
        
        // Make the XMLHttpRequest object
        var http = createRequestObject();
        
        function sendRequest(act) {
            
            // Open PHP script for requests
            http.open('get', 'ajax.php?act='+act);
            http.onreadystatechange = handleResponse;
            http.send(null);
        
        }
        
        function handleResponse() {
        
            if(http.readyState == 4 && http.status == 200){
        
                // Text returned FROM PHP script
                var response = http.responseText;
        
                if(response) {
                    // UPDATE ajaxTest content
                    document.getElementById("countLinks").innerHTML = response;
                    setTimeout(countLinks,5000);
                }
        
            }
        }
    
        function countLinks() {
            sendRequest('countLinks');
        }
    </script>
    then for the ajax.php

    Code:
    <?php
    mysql_connect("localhost", "name", "pass");
    mysql_select_db("db");
    
    if ($_GET['act'] == "countLinks") {
    
        $sql = mysql_query("select count(url) from links");
        while($count = mysql_fetch_row($sql)) {
    
            echo "<b>".$count['0']."</b>";
    
        }
    }
    ?>
    then calling the function:

    Code:
    <script type="text/javascript">
    <!--
    countLinks();
    // -->
    </script>
    Also I tried changing the get to post in both scripts and it still did not work.

    Thank you SoulScratch for your willingness to help me

  4. #4
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your code is as it is, and the php code is correctly fetching the database info, you're setting innerHTML on an element that hasn't even loaded yet (your division). I suggest wrapping the last bit of code in this..

    window.onload = function() {
    countLinks();
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •