+ Reply to Thread
Results 1 to 6 of 6

Thread: implementing AJAX ??

  1. #1
    n3v3rl0v3 is offline x10Hosting Member n3v3rl0v3 is an unknown quantity at this point
    Join Date
    Aug 2007
    Location
    karachi
    Posts
    75

    implementing AJAX ??

    I have made a simple registration page for a project in php using POST method.
    Two files are :
    • register.php ( have a form showing First Name, Last Name, Username, Password and Email fields.
    • do register.php ( checks if form has been posted and checks for the required fields. On finding any blank field it displays a message that you have left a required field. If user has filled all required fields, it shows message Registration successful.)
    What i want is to check username field on the register.php when user types his/her username and validate it without needing to submit and display messege in other page.

    I have seen some ajax scripts that loads page on a same window and validates on the spot. But i dont know javascripting and neither i tried ajax before that.

    Can anybody guide me any way for checking doregister.php on a same register.php page and show messege if user enters existed username or skips any required field????
    - Earn Double with Bux Sites:
    http://buxdeals.co.cc
    - Another Bux http://honestbux.co.cc/
    -Ptc Noob:PTC Makes Life Rich
    -Refback system: www.clickersgroup.co.cc

  2. #2
    sarvar's Avatar
    sarvar is offline x10Hosting Member sarvar is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    82

    Re: implementing AJAX ??

    Yeah! I really wanted to do this, but I cannot understand to code AJAX. I would really want a tutorial on this.
    CLICK HERE to Thank Me.
    (You must be logged in into forums.)

  3. #3
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: implementing AJAX ??

    http://w3schools.com/ajax/ajax_intro.asp


    As an FYI, even though they're using ASP for their example, it -can- be done in php. The code's different, but here:

    Code:
    xmlHttp.open("GET","time.asp",true);
    Change time.asp to time.php and use standard php functions to return the current time.

    That'll use PHP instead of ASP




    Once you're further in you'll see how they're using JS to overwrite the contents of an area on the page.

    I find it insanely easy to just have JS take whatever my php document is returning, and overwrite the contents of a <div> with that.


    So if my PHP file returns OMGWTFBBQ, it overwrites the div with OMGWTFBBQ. If it returns todays weather forecast, it'll overwrite the div with that.

    Prolly a bit overkill but it was the easy way for me to figure out WTF I was doing with AJAX



    Sadly I've no real tutorials, I just took what the w3c's example was and tore it to shreds and Frankenstein'ed it into what I'm using it for right now: reloading the Standings section of a php game I'm trying to make.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  4. #4
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: implementing AJAX ??

    Using the post method is a bit more complicated in AJAX. Here is the basic code:
    Code:
    var http_request = false;
    function makePOSTRequest(url, params) {
      http_request = false;
      if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
          http_request.overrideMimeType('text/html');
        }
      } else if (window.ActiveXObject) {
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
        }
      }
      if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
      }
    
      http_request.onreadystatechange = DispResponseText;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(params);
      return true;
    }
    
    function DispResponseText() {
      if (http_request.readyState == 4) {
        if (http_request.status == 200) {
          var response = http_request.responseText;
        
         //Stuff to do with server response (response)...
    
        } else {
    
          //Code to execute on error...
    
         alert("There was a problem with your request. Error code: " + http_request.status);
        }
      }
    }
    Here is an Explanation on the code:
    Code:
    http_request = false;
      if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
          http_request.overrideMimeType('text/html');
        }
      } else if (window.ActiveXObject) {
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
        }
      }
      if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
      }
    This tries all the different ways of creating an http object to use AJAX, and checks to see whether AJAX is supported.
    Code:
    http_request.onreadystatechange = DispResponseText;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
      return true;
    This sends the request, using the open method, using the method, url, and whether or not the code should freeze, or continue while the script is waiting for the request to come back (yes, code does continue which is recommended) parameters. The event onreadystatechange triggers the DispResponseText function when the code status changes, 1-4 and 4 being completed.
    Code:
    function DispResponseText() {
      if (http_request.readyState == 4) {
        if (http_request.status == 200) {
          var response = http_request.responseText;
        
         //Stuff to do with server response (response)...
    
        } else {
    
          //Code to execute on error...
    
         alert("There was a problem with your request. Error code: " + http_request.status);
        }
      }
    This function handles the request when the state changes, checks that it completed successfully which is marked by a readystate 4 and a 200 status. You insert your own code to handle the server response. Error handling is not needed if you feel a simple alert is sufficient. Most likely for the response handling, you just need to display it within a span element. That would be:
    Code:
    document.getElementById("status").innerHTML=response;
    With all this put together, a sample call to the server using the AJAX library is:
    Code:
    makePOSTRequest("do register.php", "Name=Steve&Year=2009&Address=Yes");
    Now put all this together into a javascript file named Twinkie.js and your good to go =)
    Last edited by Twinkie; 03-18-2009 at 05:09 PM.

  5. #5
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: implementing AJAX ??

    Wouldn't it be easier to just check the fields using javascript, and then submit it normally?
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  6. #6
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: implementing AJAX ??

    Quote Originally Posted by xav0989 View Post
    Wouldn't it be easier to just check the fields using JavaScript, and then submit it normally?
    I am guessing, since he is already adding JavaScript integration, that he wants to use a database to check the information.
    Last edited by Twinkie; 03-18-2009 at 08:38 PM.

+ Reply to Thread

Similar Threads

  1. AJAX games...
    By Sup3rkirby in forum Off Topic
    Replies: 1
    Last Post: 09-11-2008, 06:29 PM
  2. PHP and Ajax (Authentication how to ??)
    By oracle in forum Programming Help
    Replies: 3
    Last Post: 06-16-2008, 09:06 AM
  3. AJAX response xml not working
    By jspcodes in forum Programming Help
    Replies: 3
    Last Post: 06-05-2008, 08:12 AM
  4. AJAX Gaming Server?
    By Sup3rkirby in forum Programming Help
    Replies: 14
    Last Post: 12-26-2007, 04:14 PM
  5. ads included by ajax
    By agustinvinao in forum Free Hosting
    Replies: 1
    Last Post: 05-28-2006, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers