9

I have to post data from my HTML form to server in xml format, something like:

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

All I know is it goes to one of the CRM applications run on different domain. Now I'm not sure what is the best way to do this.

I was thinking of just wrapping values of fields in my form when user submits the form. So if user typed "Homer" in "firstname" field and clicks submit, my JS would change the value of the field to <firstname>Homer</firstname> and then post the data.

If it helps I'm using jQuery on client side. I think there must be the better way as my solution would break with JS disabled and seems a bit dodgy so if you could point me in the right direction that would be awesome.

Andrew
  • 18,680
  • 13
  • 103
  • 118
spirytus
  • 10,726
  • 14
  • 61
  • 75

5 Answers5

10

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
6

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> 
user2700463
  • 61
  • 1
  • 1
3

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;
});
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Both links are dead! – thomaskonrad Jan 05 '17 at 16:37
  • @thomaskonrad I've fixed the links (by redirecting to archives on wayback). It's likely some of the downloads and other resources may not work. Let me know if you encounter any further issues. – Sampson Jan 06 '17 at 21:20
2

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

Pierre
  • 34,472
  • 31
  • 113
  • 192
0

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);
kmuenkel
  • 2,659
  • 1
  • 19
  • 20