www.webdeveloper.com
Results 1 to 6 of 6

Thread: i have a problem getting my AJAX to work

  1. #1
    Join Date
    Apr 2008
    Location
    York, UK 'or' HELL lol
    Posts
    25

    i have a problem getting my AJAX to work

    hi,

    i am having a problem getting my AJAX to work...

    what i want it to do is do the function on page load, the refresh the data every second, i have messed about with the code but cannot seem to get it to do what i want...it loads the function fine on page load, but it wont refresh...

    can some one please help me with this.

    Code:
    <?php
    include('connect.php');
    $con = mysql_connect("$server","$user","$pass");
    if (!$con) {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("$db", $con);
    
    $sql="SELECT * FROM users WHERE accessid = ".$_GET['user'];
    
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
     {
    ?>
    <body onload="showDetails(<?php echo $row['username']; ?>)" />
    
    <script type="text/javascript">
    
    var xmlHttp
    
    
    function showDetails(str)
    { 
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      } 
    var url="userinfo.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }
    function stateChanged() 
    { 
    if (xmlHttp.readyState==1)
    { 
    document.getElementById("txtHint").innerHTML=="Loading";
    }
    if (xmlHttp.readyState==4)
    { 
    document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
    }
    }
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
    }
    setTimeout("showDetails(<?php echo $row['username']; ?>)", 100);
    </script>
    <span id="txtHint">
    </span>
    <?php
    }
    mysql_close($con);
    ?>
    Last edited by Vinny69; 02-25-2009 at 05:52 PM. Reason: i had body onclick not body onload...i changed it and forgot to change it back...

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    In your setTimeout function you set the number of milliseconds to 100 which equates to .1 seconds. You need to tack on one more zero for it to work every second. I am not 100&#37; sure if that is the problem.

  3. #3
    Join Date
    Apr 2008
    Location
    York, UK 'or' HELL lol
    Posts
    25
    thanks for that, unfortunately it still isnt refreshing my data

    i just dont understand why it isn't working...

  4. #4
    Join Date
    Apr 2008
    Location
    York, UK 'or' HELL lol
    Posts
    25
    i put a php RAND() code in the data and i just figured out that the script is only refreshing the data once, i want it to continue refreshing so that the data is up to date...

    i know this is possible as Windows live profiles use it, i just cannot figure it out

  5. #5
    Join Date
    Apr 2008
    Location
    York, UK 'or' HELL lol
    Posts
    25
    woooo, i got it working i was just fiddling around and its working now...

    i just changed the following part of the code:
    Code:
        }
      }
    return xmlHttp;
    }
    setTimeout("showDetails(<?php echo $row['username']; ?>)", 100);
    </script>
    To this and its now working:
    Code:
        }
      }
    setTimeout("showDetails(<?php echo $row['username']; ?>)", 100);
    return xmlHttp;
    }
    </script>

    thankyou "skywalker2208" for your help

  6. #6
    Join Date
    Apr 2008
    Location
    York, UK 'or' HELL lol
    Posts
    25
    hi again,
    how would i go about adding a bgsound when the data changes,
    but to only play once onChange
    i am using this script for my web messenger and i need it for when people sign in
    i have th folowing statuses:
    online
    busy
    away
    offline

    i only want the sound to play when the user signs in.

    even if they sign in as away or busy.

    but i dont want the sound to replay every time the user swiches between these statuses

    thanks in advance for any help.



    Vinny

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles