1

I want to be able to load an xml file from my server. Edit a node in the xml with jQuery and then save this change with the rest of the xml file back to the server using php. Anyone know how to do this? I have code that changes the xml node and can see this in my console. But cannot get the code back to my server.

Thanks!

<?php
$xml = $_POST['xml'];
$file = fopen("data.xml","w");
fwrite($file, $xml);
fclose($file);
?> 

$.post('saveXml.php', { xml: $(data)}, function(data){alert('data loaded');});

if I console.log(data) I get #document and all the xml nodes. I also get the data.xml file on my server but it's blank.

11
  • show us the code you are using that isn't working Commented Nov 23, 2012 at 19:48
  • Added code...thianks @charlietfl Commented Nov 23, 2012 at 19:54
  • var_dump your $xml and see if you are getting complete xml at php end, also check your webpages view source, sometimes xml doesn't appear on webpage Commented Nov 23, 2012 at 19:57
  • how do I var_dump my $xml - like this var_dump($xml) - also how do I check this? @Pankaj Khairnar Commented Nov 23, 2012 at 19:58
  • I get this in my console.log - don't know if it helps Uncaught TypeError: Illegal invocation Commented Nov 23, 2012 at 20:05

1 Answer 1

4

Have never done this before but found some information here: Convert xml to string with jQuery I have tested the following which will modify the original xml and send back to server as a string received by $_POST['xml']

$(function() {
    $.get('test.xml', function(xml) {
        var $xml = $(xml)
         /* change all the author names in original xml*/
        $xml.find('author').each(function() {
            $(this).text('New Author Name');

        })

        var xmlString=jQ_xmlDocToString($xml)

            /* send modified  xml string to server*/    
        $.post('updatexml.php', {xml:xmlString },function (response){             
             console.log(response)
             /* using text dataType to avoid serializing xml returned from `echo $_post['xml'];` in php*/
        }'text')
    }, 'xml')
});

function jQ_xmlDocToString($xml) {
    /* unwrap xml document from jQuery*/
    var doc = $xml[0];
    var string;
    /* for IE*/
    if(window.ActiveXObject) {
        string = doc.xml;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        string = (new XMLSerializer()).serializeToString(doc);
    }
    return string;
}

DEMO: http://jsfiddle.net/54L5g/

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

6 Comments

Does this mean I have to change my xml data to a string before I send it back to the server? @charlietfl
Could you please have a look at my demo at blinqcreative.co.uk/xml and let me know what I'm missing - my php script is in the example above. Thanks @charlietfl
you need to convert the xml doc to string as I have in jQ_xmlDocToString. You can see in demo that the code I provided works, so try using it
Ok so used your code and it seems to load and no errors...but...no xml in the new file. Is my php script correct? @charlietfl
error in console needs to be fixed. If you are going to change the xml it needs to be in $( data) so my function expects a jQuery object passed in, not the original doc
|

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.