Link to home
Start Free TrialLog in
Avatar of ITKnightMare
ITKnightMare

asked on

Extremely Urgent dynamic XML generation via classic ASP and using POST

Hi All:

I have created 2 files:

test.htm:

<html><head><title>Forms to XML</title></head>
<body><h1>Address Collector</h1>
<form action="formxml.asp" method="POST">
<p>First Name:<input type="text" name="firstname" size="20" /></p>
<p>Last Name:<input type="text" name="lastname" size="30" /></p>
<p>Email:<input type="text" name="email" size="20" /></p>

<p>Module Selected:<input type="text" name="module1" size="10" /></p>
<p>Module Expires:<input type="text" name="module1x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module1q" size="10" /></p>

<p>Module Selected:<input type="text" name="module2" size="10" /></p>
<p>Module Expires:<input type="text" name="module2x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module2q" size="10" /></p>

<p>Module Selected:<input type="text" name="module3" size="10" /></p>
<p>Module Expires:<input type="text" name="module3x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module3q" size="10" /></p>

<p>Module Selected:<input type="text" name="module4" size="10" /></p>
<p>Module Expires:<input type="text" name="module4x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module4q" size="10" /></p>

<p>Module Selected:<input type="text" name="module5" size="10" /></p>
<p>Module Expires:<input type="text" name="module5x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module5q" size="10" /></p>

<p>Module Selected:<input type="text" name="module6" size="10" /></p>
<p>Module Expires:<input type="text" name="module6x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module6q" size="10" /></p>

<p>Module Selected:<input type="text" name="module7" size="10" /></p>
<p>Module Expires:<input type="text" name="module7x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module7q" size="10" /></p>

<p>Module Selected:<input type="text" name="module8" size="10" /></p>
<p>Module Expires:<input type="text" name="module8x" size="10" /></p>
<p>Module Quizzed:<input type="text" name="module8q" size="10" /></p>

<p>Affiliation:<input type="text" name="affiliation" size="40" /></p>
<p><input type="submit" name="Submit" /></p>
</form>

</body></html>


formxml.asp:

<%@LANGUAGE="VBScript"%>
<%
Response.Buffer = True
Response.ContentType="application/xml"

Response.Write "<?xml version=""1.0"" encoding=""iso-8859-1""?>" &vbCrLf

Response.Write "<user>"
Response.Write "<fn>" & Request.Form.item("firstname") & "</fn>"
Response.Write "<ln>" & Request.Form.item("lastname") & "</ln>"
Response.Write "<email>" & Request.Form.item("email") & "</email>"

Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module1") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module1x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module1q") & "</quizzed>"
Response.Write "</module>"


Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module2") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module2x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module2q") & "</quizzed>"
Response.Write "</module>"

Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module3") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module3x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module3q") & "</quizzed>"
Response.Write "</module>"

Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module4") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module4x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module4q") & "</quizzed>"
Response.Write "</module>"

Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module5") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module5x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module5q") & "</quizzed>"
Response.Write "</module>"

Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module6") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module6x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module6q") & "</quizzed>"
Response.Write "</module>"

Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module7") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module7x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module7q") & "</quizzed>"
Response.Write "</module>"

Response.Write "<module>"
Response.Write "<mname>" & Request.Form.item("module8") & "</mname>"
Response.Write "<expires>" & Request.Form.item("module8x") & "</expires>"
Response.Write "<quizzed>" & Request.Form.item("module8q") & "</quizzed>"
Response.Write "</module>"

Response.Write "<affiliation>"
Response.Write "<affname>" & Request.Form.item("affiliation") & "</affname>"
Response.Write "</affiliation>"
Response.Write "</user>"
%>


They are pretty ugly... Since they are PoC code only :)

the idea is, someone will enter a bunch of info on a form as above, and when they click "submit" it will generate an  XML tree from the form and do a POST to another website. Now... I have 2 questions:

1) Can someone help me with tweaking the above to have cooler logic? In short, people might and might not choose a "module" and the number of modules MAY change (currently it's 8), I want it in such a way that, it will loop through the HTML forms submission, and if the module info was not entered, NOT TO INCLUDE IT IN THE XML tree.

2) How the heck do I do a post to another site in classic asp? is it done in the html page, or the asp page? can someone provide me a sample, or tweak the above code to do a post to a dummy url?

I am assigning 500 points to this becuase I need it by the end of Friday LATEST!

Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Here's a revised version of your code that uses the XML DOM to construct the XML:

<%@LANGUAGE="VBScript"%>
<%
    Response.Buffer = True
    Response.ContentType="application/xml"

    Set doc = Server.CreateObject("MSXML2.DOMDocument.4.0")
    doc.loadXml "<?xml version=""1.0"" encoding=""iso-8859-1""?><user />"

    Set root = doc.documentElement
    CreateNode "fn", Request.Form("firstname"), root
    CreateNode "ln", Request.Form("lastname"), root
    CreateNode "email", Request.Form("email"), root

    For i = 1 To 5

        If Request.Form("module" & i) <> "" Then
            Set modNode = CreateNode("module", "", root)
            CreateNode "mname", Request.Form("module" & i), modNode
            CreateNode "expires", Request.Form("module" & i & "x"), modNode
            CreateNode "quizzed", Request.Form("module" & i & "q"), modNode
        End If

    Next

    Set root = CreateNode("affiliation", "", root)
    CreateNode "affname", Request.Form("affiliation"), root

    Response.Write doc.xml
    Set doc = Nothing

    Function CreateNode( name, value, parent )

        Set newNode = doc.createElement(name)
        newNode.appendChild doc.createTextNode(value)

        If Not parent Is Nothing Then
            parent.appendChild newNode
        End If
 
        Set CreateNode = newNode

    End Function
%>
Avatar of ap_sajith
ap_sajith

Here is another version.. Think it is more generic to handle new form elemetns..

<%
Dim elem ' As Variant
Dim sXML ' As String
Dim iCount ' As Integer
iCount=1
sXML="<?xml version=""1.0"" encoding=""iso-8859-1""?>" & vbcrlf
sXML=sXML &"<user>" & vbcrlf
For each elem in request.Form
      If Instr(elem,"module")>0 then                  
            If iCount=1 Then
                  sXML=sXML & "<module>" & vbcrlf
                  sXML=sXML & "<mname>" & request.Form(elem) & "</mname>" & vbcrlf      
                  iCount=iCount+1
            ElseIf iCount=2 Then
                  sXML=sXML & "<expires>" & request.Form(elem) & "</expires>" & vbcrlf
                  iCount=iCount+1
            ElseIf iCount=3 Then
                  sXML=sXML & "<quizzed>" & request.Form(elem) & "</quizzed>" & vbcrlf
                  sXML=sXML & "</module>" & vbcrlf
                  iCount=1
            End If
      ElseIf Instr(elem,"affiliation")>0 Then
            sXML=sXML & "<affiliation>"
            sXML=sXML & "<affname>" & request.Form(elem) & "</affname>" & vbcrlf
            sXML=sXML & "</affiliation>"
      Else
            sXML=sXML & "<" & elem&">" & request.Form(elem) & "</" & elem&">" & vbcrlf
      End If      
Next
sXML=sXML & "</user>" & vbcrlf

Response.Buffer = True
Response.ContentType="application/xml"
Response.write sXML

' Send results to the new URL
Call SendResult ("http://somesite.com/someurl.asp?sxml=" & sXML)

Sub SendResult(urlStr)
  Dim xmlHttp    
  Set xmlHttp = CreateObject("Msxml2.XMLHTTP")    'create object
  On Error Resume Next               'error handling
  xmlHttp.Open "POST", urlStr, False
  'sending FORM data
  xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  xmlHttp.Send        'send HTTP request to the server
  Set xmlHttp = nothing      'free the object
End Sub
%>

I havent tested the script... So you have to test the script from your end.

Cheers!
Small correction... Response.buffer line should go at the very top otherwise it would throw an error.

Cheers!!
Avatar of ITKnightMare

ASKER

OK .... both don't work.. first doesn't work at all... Second wroks but weirdly... I made my own by mixing the two and that didn't work either.

First one doesn't work, for it throws this error:

XML Parsing Error: not well-formed
Location: http://198.82.173.28:81/formxml.asp
Line Number 1, Column 26: <font face="Arial" size=2>
------------------------------------------------------------^

The "view source" via firefox reveals this:
 <font face="Arial" size=2>
<p>Server object</font> <font face="Arial" size=2>error 'ASP 0177 : 800401f3'</font>
<p>
<font face="Arial" size=2>Server.CreateObject Failed</font>
<p>
<font face="Arial" size=2>/formxml.asp</font><font face="Arial" size=2>, line 6</font>
<p>
<font face="Arial" size=2>800401f3
</font>

********************************************************************************
The second one wroks but oddly... meaning it adds a "submit" node, and has a value in it? and the blank modules still show up... As you might recall, I had asked for the empty "non filled" modules NOT TO SHOW UP in the XML. the output generated via the second is as follows:

<user>
<firstname>asdt2t</firstname>
&#8722;
      <module>
<mname/>
<expires/>
<lastname>aasdga</lastname>
<quizzed>0</quizzed>
</module>
<email>asd@asd.com</email>
&#8722;
      <module>
<mname/>
<expires/>
<quizzed/>
</module>
&#8722;
      <module>
<mname>USA</mname>
<expires>11-11-11</expires>
<quizzed/>
</module>
&#8722;
      <module>
<mname/>
<expires/>
<quizzed/>
</module>
&#8722;
      <affiliation>
<affname>The Boston Beanery</affname>
</affiliation>
&#8722;
      <module>
<mname/>
<expires/>
<quizzed>1</quizzed>
</module>
&#8722;
      <module>
<mname>Japan</mname>
<expires>22-33-44</expires>
<quizzed/>
</module>
&#8722;
      <module>
<mname/>
<expires/>
<quizzed/>
</module>
&#8722;
      <module>
<mname/>
<expires/>
<quizzed/>
</module>
<Submit>Submit Query</Submit>
</user>

I also don't know if it submitted to the url. Supposedly the XML parser it is "posting" to has various response capabilities. I would like to get the response from it and show it as well.

******************************************************************************************************************

My mix is the following code, and it throws the same error as your first one does:

the code I have:

<%@LANGUAGE="VBScript"%>
<%
Response.Buffer = True
Response.ContentType="application/xml"

    Set doc = Server.CreateObject("MSXML2.DOMDocument.4.0")
    doc.loadXml "<?xml version=""1.0"" encoding=""iso-8859-1""?>"

    Set root = doc.documentElement
    CreateNode "fn", Request.Form("firstname"), root
    CreateNode "ln", Request.Form("lastname"), root
    CreateNode "email", Request.Form("email"), root

    For i = 1 To 8

        If Request.Form("module" & i) <> "" Then
            Set modNode = CreateNode("module", "", root)
            CreateNode "mname", Request.Form("module" & i), modNode
            CreateNode "expires", Request.Form("module" & i & "x"), modNode
            CreateNode "quizzed", Request.Form("module" & i & "q"), modNode
        End If

    Next

    Set root = CreateNode("affiliation", "", root)
    CreateNode "affname", Request.Form("affiliation"), root

    Response.Write doc.xml
    Set doc = Nothing

Call SendResult ("http://www.vbs.vt.edu/tmp/info.php?xmldata=" & sXML)

Sub CreateNode( name, value, parent )

Set newNode = doc.createElement(name)
newNode.appendChild doc.createTextNode(value)
If Not parent Is Nothing Then
parent.appendChild newNode
End If
Set CreateNode = newNode

End Sub

' Send results to the new URL

Sub SendResult(urlStr)
  Dim xmlHttp    
  Set xmlHttp = CreateObject("Msxml2.XMLHTTP")    'create object
  On Error Resume Next               'error handling
  xmlHttp.Open "POST", urlStr, False
  'sending FORM data
  xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  xmlHttp.Send        'send HTTP request to the server
  Set xmlHttp = nothing      'free the object
End Sub
%>
Form the error is sounds like you don't have XML4.0 installed. Try changing:

    Set doc = Server.CreateObject("MSXML2.DOMDocument.4.0")

To:

    Set doc = Server.CreateObject("MSXML2.DOMDocument.3.0")
ASKER CERTIFIED SOLUTION
Avatar of ap_sajith
ap_sajith

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey there ap_sajith!!

Dude you rule :)

-=::I did tweak it! As a matter of fact I'll show you the working codes::=-

-=************ HTML CODE ************=-

<html><head><title>Forms to XML</title></head>
<body><h1>Address Collector</h1>
<form action="formxml.asp" method="POST">
<p>First Name:<input type="text" name="firstname" size="20" /></p>
<p>Last Name:<input type="text" name="lastname" size="30" /></p>
<p>Email:<input type="text" name="email" size="20" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>module Selected:<input type="text" name="module" size="10" /></p>
<p>module Expires:<input type="text" name="modulex" size="10" /></p>
<p>module Quizzed:<input type="text" name="moduleq" size="10" /></p>

<p>Affiliation:<input type="text" name="affiliation" size="40" /></p>
<p><input type="submit" name="Submit" /></p>
</form>

</body></html>

-=************ END OF HTML CODE ************=-


-=************ ASP CODE ************=-
<%
Response.Buffer = True
Response.ContentType="application/xml"

Dim strXML ' As String
strXML = strXML & "<?xml version=""1.0"" encoding=""iso-8859-1""?> <!DOCTYPE user SYSTEM  ""http://www.vbs.vt.edu/xml/ecu_adduser.dtd"">" &vbCrLf


strXML = strXML & "<user>"
strXML = strXML & "<fn>" & Request.Form.item("firstname") & "</fn>"
strXML = strXML & "<ln>" & Request.Form.item("lastname") & "</ln>"
strXML = strXML & "<email>" & Request.Form.item("email") & "</email>"

FOR X = 1 to Request("module").count
      If TRIM(Request("module")(X)) <> "" Then
            strXML = strXML & "<module>"
            strXML = strXML & "<mname>" & Request.Form.item("module")(X) & "</mname>"
            strXML = strXML & "<expires>" & Request.Form.item("modulex")(X) & "</expires>"
            strXML = strXML & "<quizzed>" & Request.Form.item("moduleq")(X) & "</quizzed>"
            strXML = strXML & "</module>"
      End If
next

strXML = strXML & "<affiliation>"
strXML = strXML & "<affname>" & Request.Form.item("affiliation") & "</affname>"
strXML = strXML & "</affiliation>"
strXML = strXML & "</user>"


Response.write strXML ' Comment this out If you dont want the XML to be written out

' Send results to the new URL
strXML = strXML & vbCrLf
strXML = strXML & vbCrLf
Response.write postData("http://www.someserver.com/somepage.php","xmldata="&strXML) ' This would write out the string returned from the above URL. Comment out the response.content type line on top if the data being returned is not XML.

'==============================================================
' Function to post the form variables to the specified URL
'==============================================================
Function postData(strURL,sData)
     Dim oXMLHTTP ' As Object
     Dim strResult ' As String

     ' Create XMLHTTP Object
     '----------------------------
     Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")    
     ' error handling
     '-----------------
     'On Error Resume Next
     ' Open connection using "POST" method, asynchronously
     '-----------------------------------------------------------------
     oXMLHTTP.Open "POST", strURL, False

     ' Sending FORM data
     '------------------------
     oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
     ' Send HTTP request to the server
     '--------------------------------------
     oXMLHTTP.Send sData
     ' Error Check
     '---------------
     If Err.Number = 0 Then
          ' Wait for receive response from server
          '---------------------------------------------
          strResult = oXMLHTTP.responseText
          'Response.write strResult
          'Response.end
     Else
          ' Error Occured
          '-----------------
          strResult = "500"
     End If
     ' Cleanup
     '----------
     Set oXMLHTTP = nothing
     ' Return the response to the caller
     '---------------------------------------
     postData = strResult
End Function
%>

-=************ END OF ASP CODE ************=-
toodles :) Man.. mixed like 122 different ASP sources, and 2 book references into one simple asp page... GAWD! Sometimes, the "simplest" crap ends up being the worst don't it :p

Anyways, you get the points my friend!

Thanx for all!
Glad you got it working :).

Cheers!