1

I was browsing through other questions and posts but couldn't find the answer.

I need to send data to PHP file which saves the data to the server. Some of the data is just string variables and one of the variables is XML data. I tried to do it with the following code:

$.post(
    "save.php",
    { 
        userId: _UserId, 
        pName: _pName, 
        pId: _pId, 
        xml: $(_xml).find("main").text()
    },
    function () { 
        console.log("Saved"); 
    }
);

So _xml is an XML document and I am trying to save the entire XML in the server. The POST works but the problem is that it saves only the text, without the <> brackets.

How do I properly send the XML data to the server? Any help will be appreciated.

2 Answers 2

2

The POST works but the problem is that it saves only the text, without the <> brackets.

Try using html() instead. I know the name may be incorrect in this instance, but the underlying method will not remove elements contained within the current.

xml: $(_xml).find("main").html()
Sign up to request clarification or add additional context in comments.

1 Comment

_xml doesn't need to be a DOM element for it to work, nor does it need to contain valid HTML. Why does it not work?
0

Try this way:

$.ajax({
type : "POST",
url : "Save.php",
data : {
    method : "Save",
    userId: _UserId, 
    pName: _pName, 
    pId: _pId, 
    xml: escape(xmlString)
},
dataType : "json",
cache : false,
success : function(data) {
// Process return status data here
}
});

Note: You need to decode the xml string at server side.

4 Comments

Thanks but how do I add the rest of my variables to your function? (userId, pName, pId)
I have modified the snippet. Please have a look.
Before trying your answer I was able to post the XML with the brackets by using this function: function XmlSerialize(xml) { var s; if (typeof XMLSerializer === "function") { var x = new XMLSerializer(); s = x.serializeToString(xml); } else { s = xml.xml; } return s }
I have other problem now: the XML string which returns from the server when I read it is not full and cut in the middle. Is there a length restriction somewhere? In JS, PHP or mySql?

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.