Handling Live Web Content in WSH - Part 2
(Page 1 of 6 )
In part one of this article we began to explore the XMLHttpRequest object and how it could be used to interact with live web content. We focused primarily on reading the contents of a web page (a technique known as “screenscraping”) and parsing that data into our script. This time we are going to take a look at the rest of the methods provided by the XMLHttpObject and how we can use them to send data to a web server for remote form submission.
You may want to review part one of this article before going any further if you need to refresh your memory. The XMLHttpRequest object is a versatile tool because it’s installed by Internet Explorer (which means that it’s native to every machine on which you run your script). Its simple construct also makes coding this into your scripts as painless as possible.
We’re going to use the XMLHttpRequest object to submit POST data to a form. In order to do this we’ll need two things. The first is the full URL where we want to send our request. You can find this by looking at the source code for the form. The URL will be in the action attribute of the <form> html tag. Remember that this may be a relative reference so you may have to include the rest of the URL from your browser’s address bar. I have uploaded a sample form to use when testing this script. It is located at the following URL:
http://images.devshed.com/af/stories/hlwc2/testform.php
Next, we need to construct our POST data string. This is just a string that contains simple data field-value pairs for the form fields that we want to submit. Each data field is separated from the next by an ampersand (&) just as if we were appending it to a URL. The only difference is that we do not need to encode it, so any special characters are okay.
Our example uses two form fields: “name” and “address.” It also contains a hidden field named “action” whose value is set to “data” by default. Let’s assume we want to submit the following data in our form:
Name: John Doe
Address: 123 My Street
With this information, we would construct the following POST data string which I’ve separated across two lines for aesthetic purposes:
strPostData = “action=data&name=John Doe” _
& “&address=123 My Street”
actionURL = “http://images.devshed.com/af/stories/hlwc2/testform.php”
Next: Making the Connection >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer