How to send XML to a server through an HTML form?

I need to send data from my HTML form to the server in xml format, for example:

<some_parameters> <firstname>Homer</firstname> <lastname>Simpson</lastname> <street>74 Evergreen Tr.</street> </some_parameters> 

All I know is one of the CRM applications running in different domains. Now I'm not sure if this is the best way to do this.

I was thinking of simply wrapping the field values ​​in my form when the user submits the form. Therefore, if the user typed "Homer" in the "firstname" field and clicks "Submit", my JS will change the value of the field to <firstname>Homer</firstname> , and then publish the data.

If this helps, I use jQuery on the client side. I think there should be a better way, as my solution breaks with JS disabled and seems a little ugly, so if you can point me in the right direction, that would be awesome.

+7
jquery html xml forms
source share
5 answers

The best way I can think of is to intercept the form-submit action and convert the form data into XML format, and then send it to the server. There are many ways to do this, but the easiest way to implement the solution is through a framework like jQuery:

An example of this thing can be found on the Internet at http://www.docunext.com/...data-to-xml-with -jquery , which uses JSON to XML Plugin :

 $("#myform").submit(function(){ var formjson = $('#myform').serializeArray(); var formxml = json2xml(formjson); $.post("/collect.php", { 'data': formxml }, function(data){ // callback logic }); return false; }); 
+3
source share

Publishing XML without javascript or browser plugins is not possible. Two possible formats for placing html forms are application/x-www-form-urlencoded and multipart/form-data .

+8
source share

I just got this to work in chrome, the key has an empty space in the name of the text area:

 <html> <body> <form action="http://target_webservice" method="post"> <textarea rows="20" cols="100" name=" "> <?xml version="1.0"?><requestElements><blah></blah></requestElements> </textarea> <input type="submit" value="Submit"> </form> </body> </html> 
+4
source share

You can send XML using XFORMS . For example, see http://www.mozilla.org/projects/xforms/

+2
source share

If server-side code is an option, you can use a custom php CURL script as an intermediary to redirect your request to a third party in actual XML format. I'm not sure that CURL comes with a standard php installation, and if this is not an option, you can most likely use fsocketopen (although I personally find this tactic more complicated). But CURL is simple enough to install and extremely useful for basically allowing php to send requests as if it were a browser. The difference that you might be interested in here is that it really allows you to set the heading "Content Type: text / xml".

So your html form will send some regular GET or POST values ​​to your php script. Then have your personal PHP script convert them to the XML format expected by a third party. (Remember to use the <?xml version="1.0" encoding="ISO-8859-1"?> Tag with any attribute values ​​suitable for you.) Then send it using this code:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-type: text/xml', 'Content-length: '.strlen($xmlRequest), )); $output = curl_exec($ch); curl_close($ch); 
0
source share

All Articles