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.

1
  • In order for someone to help you, they will need to know what you are using for your backend. Commented Jan 18, 2010 at 20:11

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Do you mean possible in place of impossible?
8

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> 

1 Comment

That still posts as application/x-www-form-urlencoded
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;
});

2 Comments

Both links are dead!
@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.
2

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

2 Comments

+1 but this isn't supported on most browsers to my best knowledge.
Obsolete since Gecko 19 (Firefox 19 / Thunderbird 19 / SeaMonkey 2.16)
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.