• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 734
  • Last Modified:

Post form data via XMLHTTP

I need to post to an .asp page using XMLHTTP.

The receiving page needs some information from select boxes on the sending page in order to query a SQL server and respons back with the proper XML doc.  Can you post those values dynamically or do I have to build a string with the correct values in JavaScript first?

Example are appreciated



0
makman111
Asked:
makman111
  • 10
  • 7
  • 4
  • +2
1 Solution
 
avnerCommented:
You cannot post a form data using XMLHTTP. what you need to do is to read all the the FORM information into some sort of string then post this string using xmlhttp.

so to get the data you use :

var sValue = document.getElementById("inputField1").value;

then you create the string :

var sToSend = "inputField1="+sValue ;

send send the information using XMLHTTP :

var oXML =new ActiveXObject("MICROSOFT.XmlHttp");
oXML.Open("POST", "wheretogo.asp", true)
oXML.Send(sToSend );

Then parse the info on the ASP side.
0
 
topcat_ukCommented:
Avner,

Your example is missing the return from the asp server.

VBScript.
strStatus = oXML.status
                   
If strStatus <> 200 then
 response.write ("ERROR")
Else
 response.write (oXML.responseBODY)
End If
0
 
topcat_ukCommented:
makman111,

Here is an example from the web explaining what you are trying to do.  It is quite straight forward once you have got the basics of it.

Although there is nothing wrong with Avner's example, I would suggest that you do this server-side rather than client-side unless you are confident of your clients browser version/setup.

http://www.4guysfromrolla.com/webtech/110100-1.shtml
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BigRatCommented:
"You cannot post a form data using XMLHTTP."

Sorry, but you can.

The send() method takes an optional body. This is the body of a POSDT http request - it is considered exactly to be so, so getting it right is up to you.

The body is actually in the same format as the GET parameters, so first construct such a Javascript string :-

   var str = escape('name1') + '=' + escape(value1) +
             '&' +
             escape('name2') + '=' + escape(value2) +
             ........;

The method automatically converts BSTRs (ie: Javascript strings) into utf-8 (Unicode 16-bit chars in the string to 8-bit utf-8 chars. Since you have escaped everything (to avoid problems with = and &) this does not matter). You should now just set the content-type (with a charset if necessary) to application/x-www-form-urlencoded (the standard for POSTed forms) with the setHeader method :-

  xmlHttp.open "POST", "http://localhost/sample.xml", False
  xmlHttp.setHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")
  xmlHttp.send(str)


HTH
0
 
avnerCommented:
BigRat , If you'd read my post you'll see I suggested exactly what you wrote.
When I wrote :
"You cannot post a form data using XMLHTTP."

I meant that you cannot "submit" a form as is using XMLHTTP, you will have to use jscript/vbscript to parse outr the form data into a string and only then post it.

0
 
BigRatCommented:
Sorry, read too quickly. Now read better. One must escape the names and parameters and set the content-type. The content-length is automatically sent.
0
 
avnerCommented:
I've never had to use SetHeader in all my projects.
Also I never escaped the data, I do that on the server side anyway, why do that twice ?
0
 
BigRatCommented:
"I've never had to use SetHeader in all my projects.
Also I never escaped the data, I do that on the server side anyway, why do that twice ?"

RFC 1945 (for http 1.0) says how the posted data is to be presented to the server.

Practically the POST method allows only two encodings - x-www-form-urlencoded and multipart. Testing for multipart first and defaulting to x-www-form-urlencoded instead may occur in say ASP or Perl, or perhaps the xmlhttp object default wise sends x-ww-form-urlencoded (although this behavior is NOT documented it might actually occur) together with utf-8 as charset.

The escaping of data IS important, just like with the GET request since certain characters (= and &) are meta characters and may not appear in the data (again see the RFCs). The data is a set of name/value pairs separated by ampersands. The name is separated from the value by an equals sign. Clearly an ampersand anywhere causes incorrect name/value separation and an equal sign in a name (highly unlikely but NOT illegal) causes incorrect name/value separation. The original idea was that data posted was no different to that sent by get and one could use the same decoding routines.

When you say you do "escaping server side" you are confusing the production of URLs in HREF attributes of anchor tags. The URL is effectively a GET and for historical reasons must be pre-escaped when presented to the browser in an HTML document. This is all highly silly! The HTML document (from 4.0) is a Unicode document so there are no representational problems. Furthermore since the constructed GET URL from a <FORM> tag has all the necessary parts escaped by the browser, there would be no reason (except backwards compatability) to insist that the same URL mechansim in a HREF be pre-escaped.
0
 
avnerCommented:
BigRat, When you suggested using :
escape('name1') + '=' + escape(value1)


In what case :
escape(value1)
would be different then :
value1
?

Regarding form posting methods, I was not aware of the information you posted above, I think the reason is that on all web-servers/platforms/browsers the default type is the same and therefore I never had to explicitly declare a type.



0
 
topcat_ukCommented:
Chaps, Chaps.  Best to concentrate on the solution for makman111.  

Escaping is good as it ensures no illegal chars are sent. All data sent in a url should be encoded.
0
 
BigRatCommented:
topcat: I have posted an example of how to use the xmlhttp object with POST in accordance with the questioner's wishes. avner doubted it, which is his right! Avner and I have been here far too long and know each other far too good to get involved in a flame war.

avner: Having "abc = &" in an <INPUT> field called fred results in sending the string :-

    fred=abc+%2B+%26

having two fields (fred and joe with the same values) :-

   fred=abc+%2B+%26&joe=abc+%2B+%26

To get the data split at &, split again at = and unescape.
0
 
avnerCommented:
BigRat, I now see what you are saying, thanks for this clarfication, you seem to be very strong at the theory part, a field I sometimes tends to ignore...

makman111, I hope we did not caused you any additional head aches with this thread and that you were able to learn from BigRat's comments regarding your topic.
If you have any additional issues with your question, feel free to ask.


0
 
BigRatCommented:
"..., you seem to be very strong at the theory part"

I must admit to reading RFCs and the like in the bath!
0
 
avnerCommented:
:), You are free to do that where you want as logn as you keep me updated with the latest highlights ...
0
 
sybeCommented:
>> Practically the POST method allows only two encodings - x-www-form-urlencoded and multipart.

There are of course more options theoretically. But there is one practical option worth mentioning:

xmlHTTP.setRequestHeader "Content-Type", "text/xml"

which is of importance when using XMLRPC and probably SOAP.
0
 
makman111Author Commented:
If I use (see below) on the sending page...

  str="id=1&name=doe&location=home"
 xmlHttp.open "POST", "http://localhost/sample.xml", False
 xmlHttp.setHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")
 xmlHttp.send(str)

Can i use (see below) on the recieving page

 sID = Request("id")
 sName = Request("name")
 sLocation = Request("home")

0
 
BigRatCommented:
Yup.
0
 
makman111Author Commented:
I am getting an error (object does not support this porpery or method) on the setHeader line...  Am I missing something?

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var sMsg = "idc=" + myIDC.value + "&customer=" + myCustomer.value + "&host=" + myHost.value;
         
xmlhttp.Open("POST", "test.asp", false);
xmlhttp.setHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")
xmlhttp.Send(sMsg);
divDisplay.innerHTML=xmlhttp.responseText;
0
 
avnerCommented:
Try to run it without the set-header, it should work as the defaulted format should eb the same on all tiers.
0
 
avnerCommented:
Or you might want and try to use :

xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");

0
 
makman111Author Commented:
I have tryied it with and without the set header method...  No luck - in fact the .asp page is not able to parse any varibles.  (see code for .asp page)

<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then

     sIdc = request("idc")
     sLocation = Request("Location")
     sCustomer = Request("Customer")
     
     Response.Write( sIDC & " " & sLocation & " " & sCustomer)

End If
%>
0
 
avnerCommented:
Try this maybe :



For your Javascript code :

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var sMsg = "idc=" + myIDC.value + "&customer=" + myCustomer.value + "&host=" + myHost.value;
         
xmlhttp.Open("POST", "test.asp?"+sMsg , false);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")
xmlhttp.Send();
divDisplay.innerHTML=xmlhttp.responseText;
0
 
BigRatCommented:
Chnage

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");


into

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");

in order to get this right AND use standards conforming functionality.
0
 
makman111Author Commented:
I figured it out...  The correct answer is:

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
var sUrl = "idc=" + myIDC.value + "&customer=" + myCustomer.value + "&host=" + myHost.value;
xmlhttp.Open("POST", "test.asp", false);
xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.Send(sUrl);
divDisplay.innerHTML=xmlhttp.responseText;          
0
 
avnerCommented:
makman111 , the specific ActiveXObject you'll be launching is depends on the MSXML version installed on your machine and that is defaulted by your browser.

Happy to see you came to a solution.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 10
  • 7
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now