+ Reply to Thread
Results 1 to 6 of 6

Thread: Ajax innerHTML

  1. #1
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,055
    Blog Entries
    6

    Ajax innerHTML

    This tutorial continues my tutorial on innerHTML. In this tutorial, instead of using a function that will directly update the page, we are going to use ajax techniques to update the page. By using Ajax, you can call out to any piece of PHP (or other web language like ASP) code, rather than just be able to use javascript.

    First, we start with our base form again, including our onclick event:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <
    html>
    <
    head>
      <
    meta content="text/html; charset=ISO-8859-1"
    http-equiv="content-type">
      <
    title></title>
    </
    head>
    <
    body>
    <
    p id="namehere"></p>
    <
    input name="name" id="name"><br>
    <
    input name="Submit" id="Submit" value="Submit"
     
    onclick="javascript:changename()" type="button"></body>
    </
    html
    Next we’ll add in the function. I’ll explain what each bit of what we are doing is in the comments of the code.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <
    html>
    <
    head>
      <
    meta content="text/html; charset=ISO-8859-1"
    http-equiv="content-type">
      <
    title></title>
    </
    head>
    <
    body>
    <
    script type="text/javascript">
    function 
    changename()
    {
    //Declare the variable that will hold the XMLHttp Object.
    var ajaxobject;
    if (
    window.XMLHttpRequest)
      {
      
    // code for IE7+, Firefox, Chrome, Opera, Safari
      
    ajaxobject=new XMLHttpRequest();
      }
    else if (
    window.ActiveXObject)
      {
      
    // code for IE6, IE5
      
    ajaxobject=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      {
    //The browser doesn’t support Ajax.
      
    alert("Your browser does not support Ajax!");
      }
    //Here we store a function in the onreadystatechange property. This makes it automatically set off when the readystate changes.
    ajaxobject.onreadystatechange=function()
    {
    if(
    ajaxobject.readyState==4)
    //Here, we test if the readystate is 4, which means it is finished.
      
    {
    //Here is where we flow on from the last tutorial. We update the page by changing the innerHTML to the HTML that is sent back from the ajax request.
      
    document.getElementById('namehere').innerHTML ajaxobject.responseText;
      }
    }
    //Here is where the Ajax request sends out to an external page. You can also use POST, however you must add some more code.
    ajaxobject.open("GET","ajaxtest.php?name="+document.getElementById('name').value,true);
    //This is normally used to send extra data, like when doing a POST instead of GET.
    ajaxobject.send(null);
    }
    </script>
    <p id="namehere"></p>
    <input name="name" id="name"><br>
    <input name="Submit" id="Submit" value="Submit"
     onclick="javascript:changename()" type="button"></body>
    </html> 
    Now, we also need another file, called ajaxtest.php. All this PHP script will do is get the name sent to it and then echo it.

    Code:
    <?php
    echo "Hello, "$_GET['name'];
    ?>
    Congratulations, you have created an Ajax page. By putting your name in the text box and pressing submit it should show up without reloading the page, just like in the innerHTML tutorial. Remember though, that we have used PHP code rather than pure javascript though. In this respect, we could do things like call out to a database to get information, where as with the pure javascript solution, this is an unreasonable thing to do.
    A live demo will be available on my CodeCall subdomain.

    If this was helpful, please +rep!
    My Site | Questions and Answers | Ask Me: Termana | Last Tutorial: Ajax innerHTML
    If you can keep your head while all around you are losing theirs, you probably have a CD writer on your desktop

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Ajax innerHTML

    Very nicely done, +rep!

  3. #3
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

    Re: Ajax innerHTML

    good work
    ill +rep you when i can

  4. #4
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    Re: Ajax innerHTML

    Ajax is one of the most popular "buzz" words out there today,
    it's good to finally see some code using this technique
    +rec
    The owls are not what they seem...

  5. #5
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,702
    Blog Entries
    57

    Re: Ajax innerHTML

    Good job, +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #6
    Newbie reddem0n is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    11

    Re: Ajax innerHTML

    thank you for your time in writing this ..very helpful

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Venkat Subramaniam to Speak on Debugging Ajax, Agile Development
    By Shaguf in forum Software Development Tools
    Replies: 0
    Last Post: 11-27-2008, 12:25 AM
  2. AJAX: Intro
    By John in forum Tutorials
    Replies: 1
    Last Post: 11-07-2007, 08:52 PM
  3. Considering Ajax, Part 1: Cut through the hype
    By Ronin in forum JavaScript and CSS
    Replies: 0
    Last Post: 12-13-2006, 02:36 PM
  4. Myth-Busting AJAX (In)security
    By Jordan in forum AJAX
    Replies: 1
    Last Post: 12-03-2006, 01:21 PM
  5. AJAX Evolved
    By Jordan in forum JavaScript and CSS
    Replies: 0
    Last Post: 09-09-2006, 01:12 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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