Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP to PHP

Status
Not open for further replies.

Smoothas

IS-IT--Management
May 1, 2002
93
GB
Hello,

I new to PHP, but I need to use it as my current ISP dosn't support ASP pages ( and i cannot change ISPs at the moment).
I've trying to get web input from a standard HTML form page and convert it into an XML file to past to a datbase to interrogate, which will then supply a result back in the form of an xml page.

I have the following ASP code that takes inpu from a html form ( code also attached ), but I tried to convert it to PHP with no avail.

Could someone help me , or has another PHP way of transforming html form input into XML, that would be great.

>>>> processForm.asp
-----
<%

'--------------------------------------------------------------------
'The "ConvertFormtoXML" Function accepts to parameters.
'strXMLFilePath - The physical path where the XML file will be saved.
'strFileName - The name of the XML file that will be saved.
'--------------------------------------------------------------------

Function ConvertFormtoXML(strXMLFilePath, strFileName)

'Declare local variables.
Dim objDom
Dim objRoot
Dim objField
Dim objFieldValue
Dim objattID
Dim objattTabOrder
Dim objPI
Dim x


'Instantiate the Microsoft XMLDOM.
Set objDom = server.CreateObject("Microsoft.XMLDOM")
objDom.preserveWhiteSpace = True


'Create your root element and append it to the XML document.
Set objRoot = objDom.createElement("contact")
objDom.appendChild objRoot


'Iterate through the Form Collection of the Request Object.
For x = 1 To Request.Form.Count

'Check to see if "btn" is in the name of the form element.
'If it is, then it is a button and we do not want to add it
'to the XML document.
If instr(1,Request.Form.Key(x),"btn") = 0 Then

'Create an element, "field".
Set objField = objDom.createElement("field")

'Create an attribute, "id".
Set objattID = objDom.createAttribute("id")

'Set the value of the id attribute equal the the name of
'the current form field.
objattID.Text = Request.Form.Key(x)

'The setAttributeNode method will append the id attribute
'to the field element.
objField.setAttributeNode objattID

'Create another attribute, "taborder". This just orders the
'elements.
Set objattTabOrder = objDom.createAttribute("taborder")

'Set the value of the taborder attribute.
objattTabOrder.Text = x

'Append the taborder attribute to the field element.
objField.setAttributeNode objattTabOrder

'Create a new element, "field_value".
Set objFieldValue = objDom.createElement("field_value")

'Set the value of the field_value element equal to
'the value of the current field in the Form Collection.
objFieldValue.Text = Request.Form(x)

'Append the field element as a child of the root element.
objRoot.appendChild objField

'Append the field_value element as a child of the field elemnt.
objField.appendChild objFieldValue
End If
Next


'Create the xml processing instruction.
Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")

'Append the processing instruction to the XML document.
objDom.insertBefore objPI, objDom.childNodes(0)


'Save the XML document.
objDom.save strXMLFilePath & "\" & strFileName


'Release all of your object references.
Set objDom = Nothing
Set objRoot = Nothing
Set objField = Nothing
Set objFieldValue = Nothing
Set objattID = Nothing
Set objattTabOrder = Nothing
Set objPI = Nothing
End Function


'Do not break on an error.
On Error Resume Next


'Call the ConvertFormtoXML function, passing in the physical path to
'save the file to and the name that you wish to use for the file.
ConvertFormtoXML "c:","Contact.xml"


'Test to see if an error occurred, if so, let the user know.
'Otherwise, tell the user that the operation was successful.
If err.number <> 0 then
Response.write("Errors occurred while saving your form submission.")
Else
Response.write("Your form submission has been saved.")
End If
%>
-<<<

>>>default.html
------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html>
<head>
<title>
Contact Information
</title>
</head>
<body>
<form action="processForm.PHP" method="post">
<h3>Enter your contact information</h3>
First Name: <input type="text" id="firstName" name="firstName"><br>
Last Name: <input type="text" id="lastName" name="lastName"><br>
Address #1: <input type="text" id="address1" name="address1"><br>
Address #2: <input type="text" id="address2" name="address2"><br>
Phone Number: <input type="text" id="phone" name="phone"><br>
E-Mail: <input type="text" id="email" name="email"><br>
<input type="submit" id="btnSub" name="btnSub" value="Submit"><br>
</form>
</body>
</html>
-<<<<<<

Many Thanks inadvance
 
I presume that as you unable to run ASP your on a linux server ? If that's true you can't use the microsoft XML parser. You might be better off just creating a php script to do what you want to do with the PHP XML functions rather than convert
 
hi,
Thank you both for your advise.

Ive tried to create a php file, but I'm getting an error message :-

Parse error: syntax error, unexpected T_STRING in /freeola/users/8/1/sr1154218/htdocs/hidden/test/d2xml.php on line 36


<?php
if(isset($_POST['create_xml'])){

echo "Links Data Posted";

/* All Links data from the form is now being stored in variables in string format */

$firstName = $_POST['firstName'];

$lastName = $_POST['lastName'];

$address1 = $_POST['address1'];

$address2 = $_POST['address2'];

$phone = $_POST['phone'];

$email = $_POST['email'];

$CrqTestRequest = "Identity";

$CrqCompanyID = "TestCompany";

$CrqUserID = "TestUser";

$CrqPassword = "TestPwd";

$CrqClientRef = "TESTREF0001";

$xmlBeg1 = "<?xml version='1.0' encoding='utf-8'?>";

$CrqIntegratorID ="96C68CFD-832B-429C-A353-FECFADA64713";

$xml_document= $xmlBeg1;

$xml_document= "<soap:Envelope xmlns:xds=" xmlns:xsi=" xmlns:soap="
$xml_document= "<soap:Body>";

$xml_document .= "<PerformIDCheck xmlns="THE URL I NEED TO USE">";

$xml_document .= "<CallRequest>";

$xml_document .= "<CrqIntegratorID>";

$xml_document .= $CrqIntegratorID;

$xml_document .= "</CrqIntegratorID>";

$xml_document .= "<CrqTestRequest>";

$xml_document .= $CrqTestRequest;

$xml_document .= "</CrqTestRequest>";

$xml_document .= "<CrqCompanyID>";

$xml_document .= $CrqCompanyID;

$xml_document .= "</CrqCompanyID>";

$xml_document .= "<CrqUserID>";

$xml_document .= $CrqUserID;

$xml_document .= "</CrqUserID>";

$xml_document .= "<CrqPassword>";

$xml_document .= $CrqPassword;

$xml_document .= "</CrqPassword>";

$xml_document .= "<CrqSearchId>";

$xml_document .= $CrqSearchId;

$xml_document .= "</CrqSearchId>";

$xml_document .= "<CrqClientRef>";

$xml_document .= $CrqClientRef;

$xml_document .= "</CrqClientRef>";

$xml_document .= "<CrqApplicant>";

$xml_document .= "<AptName>";

$xml_document .= "<Surname>";

$xml_document .= $lastName;

$xml_document .= "</Surname>";

$xml_document .= "<Forename>";

$xml_document .= $firstName;

$xml_document .= "</Forename>";

$xml_document .= "<Title>";

$xml_document .= $email;

$xml_document .= "</Title>";

$xml_document .= "</AptName>";

$xml_document .= "<AptAddress>";

$xml_document .= "<AadBuilding>";

$xml_document .= $phone;

$xml_document .= "</AadBuilding>";

$xml_document .= "<AadStreet>";

$xml_document .= $address1;

$xml_document .= "</AadStreet>";

$xml_document .= "<AadPostTown>";

$xml_document .= $address2;

$xml_document .= "</AadPostTown>";

$xml_document .= "<AadPostCode>";

$xml_document .= $phone;

$xml_document .= "</AadPostCode>";

$xml_document .= "</AptAddress>";

$xml_document .= "</CrqApplicant>";

$xml_document .= "</CallRequest>";

$xml_document .= "</PerformIDCheck>";

$xml_document .= "</soap:Body>";

$xml_document .= "</soap:Envelope>";

$path_dir = "output/";

$path_dir .= $urlDoc .".xml";

/* Data in Variables ready to be written to an XML file */

$fp = fopen($path_dir,'w');

$write = fwrite($fp,$xml_document);

/* Loading the created XML file to check contents */

$sites = simplexml_load_file("$path_dir");

echo "<br> Checking the loaded file <br>" .$path_dir. "<br>";
echo "<br><br>Whats inside loaded XML file?<br>";
print_r($sites);



}

?>


>>>>

Thanks again
 
hello, ok, i've managed to figure out the error. its the double quotes , so i've change the first and last double quotes to single quotes.
The error has now more to this line

$xmlBeg1 = "<?xml version='1.0' encoding='utf-8'?>";

which gives me the same error message but I don't think the quote replacement fix is going to work here.
Any Ideas ?
 
I short_open_tags configured to be on? in that case, your XML might be mistaken for PHP code. Does this work?
Code:
$xmlBeg1 = '<' . '?xml version="1.0" encoding="utf-8"?>';

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Hi. I tryied your suggestion, but I get the same error. Line 34 reads :
$XML_documentation= $xmlbeg1;

and

$xmlbeg1 = '<' . '?XML version="1.0" encoding="utf-8"?>';

i've replaced the variable with the text itself, but I get the same error.
 
UPDATE:-

HEllo,
I've looked at the code this morning and noticed a quotes conflict on line 34 which I have corrected, and NO ERROR MESSAGE.
Great. HOWEVER, no output file is being created , and therefore displayed.
Any ideas on this new glitch ( code follow ) ?

>>>>
?php
if(isset($_POST['create_xml'])){

echo "Links Data Posted";

/* All Links data from the form is now being stored in variables in string format */

$firstName = $_POST['firstName'];

$lastName = $_POST['lastName'];

$address1 = $_POST['address1'];

$address2 = $_POST['address2'];

$phone = $_POST['phone'];

$email = $_POST['email'];

$urlDoc = "testdoc";

$CrqTestRequest = "Identity";

$CrqCompanyID = "TestCompany";

$CrqUserID = "TestUser";

$CrqPassword = "TestPwd";

$CrqClientRef = "TESTREF0001";

$xmlBeg1 = '<?xml version="1.0" encoding="utf-8"?>';

$xmlBeg2 = '<soap:Envelope xmlns:xds=" xmlns:xsi=" xmlns:soap="
$xmlBeg4 = '<URL I NEED TO USE>';

$CrqIntegratorID ="96C68CFD-832B-429C-A353-FECFADA64713";

$xml_document= $xmlBeg1;

$xml_document= $xmlBeg2;

$xml_document= "<soap:Body>";

$xml_document= $xmlBeg4;

$xml_document .= "<CallRequest>";

$xml_document .= "<CrqIntegratorID>";

$xml_document .= $CrqIntegratorID;

$xml_document .= "</CrqIntegratorID>";

$xml_document .= "<CrqTestRequest>";

$xml_document .= $CrqTestRequest;

$xml_document .= "</CrqTestRequest>";

$xml_document .= "<CrqCompanyID>";

$xml_document .= $CrqCompanyID;

$xml_document .= "</CrqCompanyID>";

$xml_document .= "<CrqUserID>";

$xml_document .= $CrqUserID;

$xml_document .= "</CrqUserID>";

$xml_document .= "<CrqPassword>";

$xml_document .= $CrqPassword;

$xml_document .= "</CrqPassword>";

$xml_document .= "<CrqSearchId>";

$xml_document .= $CrqSearchId;

$xml_document .= "</CrqSearchId>";

$xml_document .= "<CrqClientRef>";

$xml_document .= $CrqClientRef;

$xml_document .= "</CrqClientRef>";

$xml_document .= "<CrqApplicant>";

$xml_document .= "<AptName>";

$xml_document .= "<Surname>";

$xml_document .= $lastName;

$xml_document .= "</Surname>";

$xml_document .= "<Forename>";

$xml_document .= $firstName;

$xml_document .= "</Forename>";

$xml_document .= "<Title>";

$xml_document .= $email;

$xml_document .= "</Title>";

$xml_document .= "</AptName>";

$xml_document .= "<AptAddress>";

$xml_document .= "<AadBuilding>";

$xml_document .= $phone;

$xml_document .= "</AadBuilding>";

$xml_document .= "<AadStreet>";

$xml_document .= $address1;

$xml_document .= "</AadStreet>";

$xml_document .= "<AadPostTown>";

$xml_document .= $address2;

$xml_document .= "</AadPostTown>";

$xml_document .= "<AadPostCode>";

$xml_document .= $phone;

$xml_document .= "</AadPostCode>";

$xml_document .= "</AptAddress>";

$xml_document .= "</CrqApplicant>";

$xml_document .= "</CallRequest>";

$xml_document .= "</PerformIDCheck>";

$xml_document .= "</soap:Body>";

$xml_document .= "</soap:Envelope>";

$path_dir = "output/";

$path_dir .= $urlDoc .".xml";

/* Data in Variables ready to be written to an XML file */

$fp = fopen($path_dir,'w');

$write = fwrite($fp,$xml_document);

/* Loading the created XML file to check contents */

$sites = simplexml_load_file("$path_dir");

echo "<br> Checking the loaded file <br>" .$path_dir. "<br>";
echo "<br><br>Whats inside loaded XML file?<br>";
print_r($sites);



}

?>

<<<

Thanks again for helping a PHP noobie
 
hello,
I figured out what was causing no output ( bad code from the HML side of things ).

Many Thanks for everyones help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top