SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    another Ajax problem...

    i am very new at ajax and i am following the tutorials in W3Scools and i am stuck. i was trying to work with very basic ajax that is suppose to connect to an asp that looks like this:
    Code:
    <%
    response.expires=-1
    response.write(time)
    %>
    i am not sure this is enough to be asp but this is there example and the ajax is:
    Code:
    <body>
    <script type="text/javascript">
    function ajaxFunction()
      {  var xmlHttp;
      try
        {    // Firefox, Opera 8.0+, Safari    xmlHttp=new XMLHttpRequest();    }
      catch (e)
        {    // Internet Explorer    try
          {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
        catch (e)
          {      try
            {        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        }
          catch (e)
            {        alert("Your browser does not support AJAX!");        return false;        }      }    }
        xmlHttp.onreadystatechange=function()
          {
          if(xmlHttp.readyState==4)
            {
            document.myForm.time.value=xmlHttp.responseText;
            }
          }
        xmlHttp.open("GET","time.asp",true);
        xmlHttp.send(null);  }</script>
    	<form name="myForm">
    Name: <input type="text"
    onkeyup="ajaxFunction();" name="username" />
    Time: <input type="text" name="time" />
    </form></body>
    can anybody help me with this?

  2. #2
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly what is the problem? Do you not get the desired output? I'm not sure I understand why you are stuck?
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem is that once you add something in the left box it should get the time and it doesnt, i here is the link:http://www.didgim.com/testAjax.htm
    ofcourse in the W3Scools site it works: http://www.w3schools.com/ajax/ajax_serverscript.asp.
    and since at the end i basicly copied there example i guess missed a few things, and i just dont know what?

  4. #4
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... having a look at it now... will get back to ya!
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  5. #5
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay... firstly, before I even got into looking at the functionality, FireFox spat out like 9 js errors... that would be 7 for every keyUp event, and two other ones for some other reason, so there's obviously a problem. I'll have a look at your code... just gimmie a sec
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  6. #6
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok... before I get started on this... lets look at the following code:

    Code:
    <html>
        <body>
            <script type="text/javascript">
                function ajaxFunction() {  var xmlHttp;
                      try {    // Firefox, Opera 8.0+, Safari    xmlHttp=new XMLHttpRequest();    }
                      catch (e) {    // Internet Explorer    try
                          {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
                        catch (e)
                          {      try
                            {        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        }
                          catch (e)
                            {        alert("Your browser does not support AJAX!");        return false;        }      }    }
                        xmlHttp.onreadystatechange=function()
                          {
                      if(xmlHttp.readyState==4)
            {
            document.myForm.time.value=xmlHttp.responseText;
            }
          }
        xmlHttp.open("GET","time.asp",true);
        xmlHttp.send(null);  }
            </script>
            
            
        <form name="myForm">
    Name: <input type="text"
    onkeyup="ajaxFunction();" name="username" />
    Time: <input type="text" name="time" />
    </form></body>
    </html>
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  7. #7
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    firstly, if you've copied this code from w3... I'm not in any way criticizing their code... but you've got to try and keep your scripts within your head tag, which is non-existent in this document, also most likely the reason for the second js error, which returns "ajaxFunction is not defined".

    I'm going to rewrite the code... then I will paste the changes here
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  8. #8
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just before I do, and just so that you could see the first problem, and understand the issue, look at the following:

    Code:
    function ajaxFunction()
      {  var xmlHttp;
      try
        {    // Firefox, Opera 8.0+, Safari    xmlHttp=new XMLHttpRequest();    }
      catch (e)
        {    // Internet Explorer    try
          {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
        catch (e)
          {      try
            {        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        }
          catch (e)
            {        alert("Your browser does not support AJAX!");        return false;        }      }    }
        xmlHttp.onreadystatechange=function()
          {
          if(xmlHttp.readyState==4)
            {
            document.myForm.time.value=xmlHttp.responseText;
            }
          }
        xmlHttp.open("GET","time.asp",true);
        xmlHttp.send(null);  }
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  9. #9
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    line number 4:

    Code:
    // Firefox, Opera 8.0+, Safari     xmlHttp = new XMLHttpRequest }
    Right infront of this line, the two forward slashes states that it's a commented line. But if you look carefully, on that same line, it's trying to define the xmlHttp object... also commented out. It has a closing bracket at the end of the line too, which is actually supposed to be seen by JavaScript, closing the "try" statement.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  10. #10
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace your code with the following, and check it again

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title></title>

    <script type="text/javascript">
    function ajaxFunction() {
    var xmlHttp;
    try {
    xmlHttp=new XMLHttpRequest();
    } catch(e) {
    try {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
    try {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
    alert("Your browser does not support AJAX!");
    return false;
    }
    }
    }
    xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {
    document.myForm.time.value=xmlHttp.responseText;
    }
    }
    xmlHttp.open("GET", "time.asp", true);
    xmlHttp.send(null);
    }
    </script>

    </script>

    </head>
    <body>

    <form name="myForm">
    Name: <input type="text"
    onkeyup="ajaxFunction();" name="username" />
    Time: <input type="text" name="time" />
    </form>

    </body>
    </html>
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  11. #11
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me know how it goes!
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    it works!

    thanks i still need to look into it more carefully. i just saw the mistake in line 4 with the closing bracket and i still nees to see what your changes are..
    but it is working...

  13. #13
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    groovy... let me know if you get stuck somewhere
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.


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
  •