SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Threaded View

  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    0 Post(s)
    0 Thread(s)

    Cant make setTimeout() AJAX work in my PHP

    Hello there. I am sooo despared since I cant make this work here is what I have in my logged_in.php file .
    PHP Code:
        <?php  echo "Welcome: "."<b>".$_SESSION["username"]."<b/>";  ?>
            <div id="ajax"></div>
                                <script type="text/javascript">
                            function createXMLHttpRequest(){
                                var XMLHttpRequestObject=false;
                                          XMLHttpRequestObject=new XMLHttpRequest();
                                           return XMLHttpRequestObject;
                            } // 
                                        return XMLHttpRequestObject;
                                           return false;
                                        }// End "else"  statement

                                } // End of the last "else"  statement
                          }   //  End "createXMLHttpObject()" function
                          function insertMessages(){
                              if(HttpObject.readyState==4 && HttpObject.status==200){



                          } // End  "insertMessages()" function
                         // insertMessages();
                          // The below is the original "insertData()" function
                          // ------------------------------------------------------------------------
                          function insertData(user,time,message){
                              if(HttpObject.readyState==4 && HttpObject.status==200){
                                 // document.getElementById("ajax").innerHTML=HttpObject.responseText;
                                 // document.getElementById("textarea").value+=HttpObject.responseText;

                          } // End  "insertData()" function
                      // ------------------------------------------------------------------------
                        </script> <!--  End  The first script tag for  Ajax related functions  -->
                var SESSION="<?php echo $_SESSION["username"]; ?>";
              function currentTime(){
               var date=new Date();
               var hours=date.getHours();
               var minutes=date.getMinutes();
               var seconds=date.getSeconds();
               var fullTime=hours+":"+minutes+":"+seconds;
                return fullTime;

              } // End "currentTime()"
                function addText(){
                    document.getElementById("textarea").value+=SESSION+"  "+currentTime()+'\n';
                function send(){
                } //  End "send()"  function


                        <style> #textarea { background-color:#F3F3F3;}</style>

            <TEXTAREA id="textarea" COLS="80" ROWS="20"   ></TEXTAREA><br/><br/>
            <input type="text" id="text" name="text"   size="96"> 
            <input type="button" value="Send" name="submit"  onclick="send()"> 


    And the request as I hope you saw is gonna go to ajax_response.php file . ANd inside that file I have
    PHP Code:

    //   PHP  Configuration
    defined("DB_HOST") ?      nulldefine("DB_HOST","localhost");
    defined("DB_USER")   ?    nulldefine("DB_USER","root");
    defined("DB_PASS")   ?    nulldefine("DB_PASS","");
    defined("DB_NAME")   ?    nulldefine("DB_NAME","livechat");

    // 1 . Creating the connection
    "Couldnt create a database connection".  mysql_error());

    // End "if"  for  creating connection
          // 2. Selecting  a database
    "Could not select the database:".mysql_error());

    //  End "if"   for selecting a database
          // 3. Perform query to the selected database
    if(isset($_GET["user"]) && isset($_GET["time"]) && isset($_GET["message"])){ //checking to see if the all $_GET variables have been set 
    $query="INSERT INTO chat(user,time,message) VALUES('{$user}','{$time}','{$message}')";
    $query="SELECT*FROM chat";
    // 4. Handling with the returned data
    //  Things for "ten_counter"
    $query="SELECT*FROM ten_counter WHERE id=1";
    $ten_counter=$fetched["ten_counter"];// 1
            // End things for "ten_counter"
    $query="DELETE FROM chat WHERE id<'{$ids_to_delete}'";
    $deleted_rows){ die("Couldnt perform the query".mysql_error());}
    mysql_query("UPDATE ten_counter SET ten_counter='{$ten_counter}' WHERE id=1");
    // Delete 10 rows
    //  End the "if"  for checking to see if the all $_GET variables have been set 
    function loadMessages(){
    $query="SELECT*FROM chat ORDER BY id ASC";
    "Query FAILED!!!".mysql_error());
    //array_push($chat,$row["user"]." ".$row["time"]."\n".$row["message"]." "."\n");
    echo $row["user"]." ".$row["time"]."\n".$row["message"]." "."\n";
    //   End  "loadMessages()" function

    //  5. Closing the connection

    My question is why setTimeout("insertMessages()",1000); doesnt work : Iexpect it every 1 second go to the database take all the rows and come and output in my text area so that people that are chatting will get the answers instantly.. I hope you understood my question ..
    And please I really need your help .. Deeply appreciate your time spent on this .
    Last edited by ScallioXTX; Nov 4, 2011 at 16:28. Reason: changed quotes to php tags


Posting Permissions

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