News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

Real Form Example

Started by ben2ong2, October 06, 2006, 04:40:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

Now, we will look at a real HTML form example.
We will first look at the HTML form that will be used in this example: The HTML form below asks for the user's name, country, and e-mail address. This information will then be written to an XML file for storage.
"customers.htm":
<html><body><form action="saveForm.asp" method="post"><p>Enter your contact information</p>First Name: <input type="text" id="fname" name="fname">
Last Name: <input type="text" id="lname" name="lname">
Country: <input type="text" id="country" name="country">
Email: <input type="text" id="email" name="email">
<input type="submit" id="btn_sub" name="btn_sub" value="Submit"><input type="reset" id="btn_res" name="btn_res" value="Reset"></form></body></html>
The action for the HTML form above is set to "saveForm.asp". The "saveForm.asp" file is an ASP page that will loop through the form fields and store their values in an XML file:
<%dim xmlDocdim rootEl,fieldName,fieldValue,attIDdim p,i'Do not stop if an error occursOn Error Resume NextSet xmlDoc = server.CreateObject("Microsoft.XMLDOM")xmlDoc.preserveWhiteSpace=true'Create a root element and append it to the documentSet rootEl = xmlDoc.createElement("customer")xmlDoc.appendChild rootEl'Loop through the form collectionfor i = 1 To Request.Form.Count  'Eliminate button elements in the form  if instr(1,Request.Form.Key(i),"btn_")=0 then    'Create a field and a value element, and an id attribute    Set fieldName = xmlDoc.createElement("field")    Set fieldValue = xmlDoc.createElement("value")    Set attID = xmlDoc.createAttribute("id")    'Set the value of the id attribute equal to the name of    'the current form field    attID.Text = Request.Form.Key(i)    'Append the id attribute to the field element    fieldName.setAttributeNode attID    'Set the value of the value element equal to    'the value of the current form field    fieldValue.Text = Request.Form(i)    'Append the field element as a child of the root element    rootEl.appendChild fieldName    'Append the value element as a child of the field element    fieldName.appendChild fieldValue  end ifnext'Add an XML processing instruction'and insert it before the root elementSet p = xmlDoc.createProcessingInstruction("xml","version='1.0'")xmlDoc.insertBefore p,xmlDoc.childNodes(0)'Save the XML You are not allowed to view links. Register or Login "c:\Customer.xml"'Release all object referencesset xmlDoc=nothingset rootEl=nothingset fieldName=nothingset fieldValue=nothingset attID=nothingset p=nothing'Test to see if an error occurredif err.number<>0 then  response.write("Error: No information saved.")else  response.write("Your information has been saved.")end if%>
Note: If the XML file name specified already exists, it will be overwritten!
The XML file that will be produced by the code above will look something like this ("Customer.xml"):
<?xml version="1.0" ?><customer>  <field id="firstName">    <value>Hege</value>   </field>  <field id="lastName">    <value>Refsnes</value>   </field>  <field id="country">    <value>Norway</value>   </field>  <field id="email">    <value>mymail@myaddress.com</value>   </field></customer>
You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login

proxious