I have to post data from my HTML form to server in xml format, something like:
Homer
If servers-side code is an option, you could use a custom php CURL script as a middleman to forward your request on to the third party in an actual xml format. I'm not sure if CURL comes with a standard php installation, and if it's not an option, you could probably use fsocketopen instead (though personally I think that tactic is harder). But CURL is easy enough to install and extremely useful for basically allowing php to send requests as if it's a browser. The difference that you may be interested in here, is that it does actually allow you to set the header 'Content-type: text/xml'.
So have your html form send off some regular GET or POST values to your php script. Then have that personal php script convert them into the XML format that the 3rd party is expecting. (Don't forget to precede it with the <?xml version="1.0" encoding="ISO-8859-1"?>
tag, with whatever attribute values are appropriate for you.) And then send it off via 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);
The best way I can think of is to intercept the form-submit action, and convert the form details into XML format, and then submit that to the server. There are many ways to do this, but the easiest would be to implement a solution via a framework like jQuery:
An example of this very thing can be found online at http://www.docunext.com/...data-to-xml-with-jquery which utilizes the 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;
});
I just got this to work in chrome, the key is having the blank space in the text area name:
<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>
You can send a XML using XFORMS. For example see: http://www.mozilla.org/projects/xforms/
Posting XML without javascript or browser plugins is impossible. The two possible formats of posting html forms are application/x-www-form-urlencoded
and multipart/form-data
.