Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get a AJAX code to work

Posted on 2007-09-29
2
Medium Priority
?
634 Views
Last Modified: 2010-08-05
Hi out there!
I'm trying to code a AJAX application, which I'm new to, and can't get it to work ok.
This code return error incompatible types at row: http.open("GET", modurl, true);
But when I had the code in a separate file it was the row http.onreadystatechange = useHttpResponse(x);
that returned the same error.
So I am not sure of which code it is that generate the error.
file test.htm:
<html><head>
<meta http-equiv="Content-Language" content="sv">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
<script language="javascript" >

function getXMLHTTPRequest() {
    var req = false;
    try { req = new XMLHttpRequest(); /* e.g. Firefox */ }
    catch(e) {
      try { req = new ActiveXObject("Msxml2.XMLHTTP"); /* Andra versioner av IE */ }
      catch(e) {
          try { req = new ActiveXObject("Microsoft.XMLHTTP"); /* Andra versioner av IE */ }
          catch (e) { req = false; }
        }}
    return req; }

  var http  = getXMLHTTPRequest();

  function getServerText(updateURL, updateId, updateName, updateValue, updateField, waitMessage)  {
    var x = document.getElementById(updateId)
    var myurl = updateURL+'?id='+updateId+'&value='+updateValue+'&field='+updateField
    myRand = parseInt(Math.random()*999999999999999);
    var modurl = myurl+"&rand="+myRand;
    http.open("GET", modurl, true);
    http.onreadystatechange = useHttpResponse(x);
    http.send(null);
  }

  function useHttpResponse(updateiId)
  {
    var y = document.getElementById(updateiId.id)
    if (http.readystate == 4) {
      if (http.status == 200)  {
        var mytext = http.responseText;
        y.value = mytext;
    }
      }  else {
       y.value = "WAIT...";
         }
  }
</script>

</head><body>
<form name="r">
<input id="P13" name="P_13" value="" onchange="getServerText('PP_RealTimeUpdate_Database.asp',this.id,this.name,this.value,'price','Uppdaterar...')">
</form>
</body></html>

I'd realy appreciate a tip of how to modify the code to get it to work.
The input filed(s) in the full scale application are very many (can be more than 1000) with id's from records in a database, and that id shall be returned to the server when cursor is living the input field, to update the databse with the fields value in realtime, without clicking any buttons. That is why I try to get this basic code to work for one field in this way!

The file 'PP_RealTimeUpdate_Database.asp' does only execute the line in this test:
<% Response.Write "ID:" & Request.QueryString("id") & "Val:" & Request.QueryString("value") & " Field:" & Request.QueryString("field") %>
to a string which will be returned when the application works ok.

Thanks in advance!
Zeth
0
Comment
Question by:dfghjk
2 Comments
 
LVL 15

Accepted Solution

by:
babuno5 earned 2000 total points
ID: 19984614
Try this i think this should work
<html><head>
<meta http-equiv="Content-Language" content="sv">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
<script language="javascript" >

function getXMLHTTPRequest() {
    var req = false;
    try { req = new XMLHttpRequest(); /* e.g. Firefox */ }
    catch(e) {
      try { req = new ActiveXObject("Msxml2.XMLHTTP"); /* Andra versioner av IE */ }
      catch(e) {
          try { req = new ActiveXObject("Microsoft.XMLHTTP"); /* Andra versioner av IE */ }
          catch (e) { req = false; }
        }}
    return req; }

  var http  = getXMLHTTPRequest();
var glb_str_x;
  function getServerText(updateURL, updateId, updateName, updateValue, updateField, waitMessage)  {
    glb_str_x = document.getElementById(updateId)
    var myurl = updateURL+'?id='+updateId+'&value='+updateValue+'&field='+updateField
    myRand = parseInt(Math.random()*999999999999999);
    var modurl = myurl+"&rand="+myRand;
    http.open("GET", modurl, true);
    http.onreadystatechange = useHttpResponse;
    http.send(null);
  }

  function useHttpResponse()
  {
    var y = document.getElementById(glb_str_x.id)
    if (http.readystate == 4) {
      if (http.status == 200)  {
        var mytext = http.responseText;
        y.value = mytext;
    }
      }  else {
       y.value = "WAIT...";
         }
  }
</script>

</head><body>
<form name="r">
<input id="P13" name="P_13" value="" onchange="getServerText('PP_RealTimeUpdate_Database.asp',this.id,this.name,this.value,'price','Uppdaterar...')">
</form>
</body></html>
0
 

Author Comment

by:dfghjk
ID: 19984981
Thanks very much babuno5!
As very often in this world, the genious and good solutions is so simple and obvious, that one ask itself why one could not find it by itself!
Thanks again! Now I have something good working to build on!
Zeth
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This article discusses how to implement server side field validation and display customized error messages to the client.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question