?
Solved

Post form data via XMLHTTP

Posted on 2003-03-18
25
Medium Priority
?
727 Views
Last Modified: 2008-01-16
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
Comment
Question by:makman111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 4
  • +2
25 Comments
 
LVL 14

Expert Comment

by:avner
ID: 8158002
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
 
LVL 1

Expert Comment

by:topcat_uk
ID: 8158049
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
 
LVL 1

Expert Comment

by:topcat_uk
ID: 8158072
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
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!

 
LVL 27

Expert Comment

by:BigRat
ID: 8158286
"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
 
LVL 14

Expert Comment

by:avner
ID: 8158306
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
 
LVL 27

Expert Comment

by:BigRat
ID: 8158381
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
 
LVL 14

Expert Comment

by:avner
ID: 8158412
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
 
LVL 27

Expert Comment

by:BigRat
ID: 8158644
"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
 
LVL 14

Expert Comment

by:avner
ID: 8158675
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
 
LVL 1

Expert Comment

by:topcat_uk
ID: 8158951
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
 
LVL 27

Expert Comment

by:BigRat
ID: 8159063
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
 
LVL 14

Expert Comment

by:avner
ID: 8159116
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
 
LVL 27

Expert Comment

by:BigRat
ID: 8159200
"..., you seem to be very strong at the theory part"

I must admit to reading RFCs and the like in the bath!
0
 
LVL 14

Expert Comment

by:avner
ID: 8159232
:), You are free to do that where you want as logn as you keep me updated with the latest highlights ...
0
 
LVL 28

Expert Comment

by:sybe
ID: 8159465
>> 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
 

Author Comment

by:makman111
ID: 8160312
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
 
LVL 27

Expert Comment

by:BigRat
ID: 8160747
Yup.
0
 

Author Comment

by:makman111
ID: 8164771
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
 
LVL 14

Expert Comment

by:avner
ID: 8164791
Try to run it without the set-header, it should work as the defaulted format should eb the same on all tiers.
0
 
LVL 14

Expert Comment

by:avner
ID: 8164806
Or you might want and try to use :

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

0
 

Author Comment

by:makman111
ID: 8164845
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
 
LVL 14

Accepted Solution

by:
avner earned 750 total points
ID: 8164890
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
 
LVL 27

Expert Comment

by:BigRat
ID: 8165705
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
 

Author Comment

by:makman111
ID: 8168356
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
 
LVL 14

Expert Comment

by:avner
ID: 8171776
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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 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