DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Ajax please help me

  1. #1
    Join Date
    Oct 2008
    Posts
    2

    Ajax please help me

    This is a file called test.php which is calling the file time_test.php.
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>PHP + Ajax auto refresh DIV</title>
    <script>
    var xmlhttp = false ;
    
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
    {
      try {
            xmlhttp = new XMLHttpRequest ();
      }
      catch (e) {
      xmlhttp = false}
    }
    function myXMLHttpRequest ()
    {
      var xmlhttplocal;
      try {
            xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
      catch (e) {
            try {
            xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
            catch (E) {
              xmlhttplocal = false;
            }
      }
    
      if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
            try {
              var xmlhttplocal = new XMLHttpRequest ();
            }
            catch (e) {
              var xmlhttplocal = false;
            }
      }
      return (xmlhttplocal);
    }
    var mnmxmlhttp = Array ();
    var mnmPrevColor = Array ();
    var responsestring = Array ();
    var myxmlhttp = Array ();
    var responseString = new String;
    
    var i=0;
    var ii = 0;
    function ajax_update()
    {
    
            url = "time_test.php";
            target2 = document.getElementById ('content');
    
            ii = i++;
    
            var content = "i=" + ii ;
    
            mnmxmlhttp = new myXMLHttpRequest ();
            if (mnmxmlhttp) {
                            mnmxmlhttp.open ("POST", url, true);
                            mnmxmlhttp.setRequestHeader ('Content-Type',
                                               'application/x-www-form-urlencoded');
       mnmxmlhttp.send (content);
                            errormatch = new RegExp ("^ERROR:");
    
                            target2 = document.getElementById ('content');
    
                            mnmxmlhttp.onreadystatechange = function () {
                                    if (mnmxmlhttp.readyState == 4) {
                                            mnmString = mnmxmlhttp.responseText;
    
                                            if (mnmString.match (errormatch)) {
                                                    mnmString = mnmString.substring (6, mnmString.length);
    
                                                    target = document.getElementById ('content');
                                                    target2.innerHTML = mnmString;
    
                                            } else {
                                                    target = document.getElementById ('content');
                                                    target2.innerHTML = mnmString;
    
                                            }
                                    }
                            }
                    }
    
    
            setTimeout('ajax_update()',300);
    }
    
    </script>
    
    <body onload="ajax_update();">
    <div id="content">Loading<div>
    
    </body>
    
    </html>
    time_test.php
    Code:
    $tim=time();
    print_r($tim);
    1) Can I include time_test.php with test.php and make it a single file instead of 2 different file.

    How to change the ajax script if the time_test.php is also inclded in the same file??

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Welcome to DevX

    On the surface I don't see why you can't take those two lines and add them to the other page. Did you try?

  3. #3
    Join Date
    Oct 2008
    Posts
    2
    Hi,

    I have the php pages where all the functions is writen in page called func.php

    The functions are called in another page which the values are returned in arrays. Now I have to make all the pages ajax enabled.

    How can I do it.

    Code:
    include("./func.php");
    
    $name       = Names(1);
    array_shift($name);
    $timeval       = $name[0];
    
    
    <HTML>
    <HEAD>
    </HEAD>
    <BODY leftMargin=0 topMargin=0>
    echo "<div style=\"position:absolute; left: 500px; width:365px; top: 230px\" align=center><font size=1 color=#a41325 face=Arial>The last time is on $timeval</font></div>";
    </body>
    </html>
    The mysql is used to store the records. To print the last time i have to fetch the data from database.
    Untill I hit the refresh key the page is not refreshed. so using ajax how to refresh the entire page without hitting the refresh button or F5 key.

    Please help me as I am finding too difficult

  4. #4
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    As for your first question, displaying a page and making AJAX request to the same page is slightly advanced. I suggest you not do it. As for your second question, the include("./func.php") line is not going to have an effect on whether you make Ajax calls or not. But you will have to restructure your pages and separate pages that do content display and PHP that just sends (XML) data.

    To refresh, you cannot use Ajax. Ajax is just a method of communication. You use JavaScript to refresh the page. Write a line of JavaScript that does a location (like history.go(0)).
    You can also use the following meta HTML tag:
    <meta http-equiv="refresh" content="2">
    where the value of content is the time to wait before refresh in seconds.

Similar Threads

  1. Ajax and Browser Positioning
    By dbrook007 in forum AJAX
    Replies: 0
    Last Post: 04-22-2008, 07:40 AM
  2. Ajax within Ajax generated code
    By geekdesign in forum AJAX
    Replies: 2
    Last Post: 09-16-2007, 08:38 PM
  3. using an AJAX function to return a value
    By k285715 in forum AJAX
    Replies: 0
    Last Post: 06-18-2007, 11:06 AM
  4. Basic AJAX examples
    By Frefaln in forum AJAX
    Replies: 2
    Last Post: 06-03-2006, 07:45 PM
  5. New forum + some AJAX links
    By Lori in forum AJAX
    Replies: 0
    Last Post: 02-09-2006, 02:26 PM

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center