+ Reply to Thread
Results 1 to 6 of 6

Thread: Ajax innerHTML

  1. #1
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    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!
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Ajax innerHTML

    Very nicely done, +rep!

  4. #3
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Ajax innerHTML

    good work
    ill +rep you when i can
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  5. #4
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    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

  6. #5
    Join Date
    Jul 2006
    Posts
    16,475
    Blog Entries
    75
    Rep Power
    143

    Re: Ajax innerHTML

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

  7. #6
    reddem0n is offline Newbie
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    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. loading the javascript in innerHTML
    By tianshiz in forum AJAX
    Replies: 1
    Last Post: 02-15-2010, 11:22 AM
  2. can php do this or do I need ajax?
    By jimiwa in forum PHP Development
    Replies: 3
    Last Post: 11-03-2009, 01:13 AM
  3. PHP Vs AJAX
    By Megrisoft in forum AJAX
    Replies: 35
    Last Post: 10-04-2009, 02:02 AM
  4. innerHTML
    By Termana in forum JavaScript Tutorials
    Replies: 3
    Last Post: 09-17-2009, 07:36 PM
  5. Ajax Pop-Up
    By Divya in forum AJAX
    Replies: 2
    Last Post: 01-07-2009, 08:06 PM

Tags for this Thread

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