How do I save HTML form data to an XML file using JavaScript? -
i making blog in users can leave comments on posts. used javascript read data xml file , display in div element. however, can't page save new comments xml file, , rewrite div contents based on changed xml file. know how edit xml file, how save it? (sorry, code's little messy)
source code:
index.html:
<!doctype html> <html lang = "en-us"> <head> <title>blog</title> <script src = "loadxml.js"> </script> <script> function addcomment1(form) { var xmldoc = loadxmldoc("one.xml"); var use = form.user1.value; var com = form.comment1.value; } </script> <meta charset = "utf-8"/> </head> <body> <h1>posts</h1> <br/> <!-- post --> <h2>comments:</h2> <div> <p> <script> var xmldoc = loadxmldoc("one.xml"); var cap = xmldoc.getelementsbytagname("content"); (i = 0; < cap.length; ++) { document.writeln(xmldoc.getelementsbytagname("user")[i].firstchild.nodevalue + ":<br/>"); document.writeln(xmldoc.getelementsbytagname("content")[i].firstchild.nodevalue + "<br/><hr/>"); } </script> </p> <form name = "form1"> <input type = "text" name = "user1"/> <input type = "text" name = "comment1" /> <input type = "submit" value = "submit" onclick = "addcomment1(this.form)"/> </form> </div> </body> </html>
one.xml:
<?xml version="1.0" encoding="utf-8"?> <comment> <user>gabe rust</user> <content>hello world!</content> </comment>
loadxml.js:
function loadxmldoc(filename) { if (window.xmlhttprequest) { xhttp=new xmlhttprequest(); } else // code ie5 , ie6 { xhttp=new activexobject("microsoft.xmlhttp"); } xhttp.open("get",filename,false); xhttp.send(); return xhttp.responsexml; } function loadxmlstring(txt) { if (window.domparser) { parser=new domparser(); xmldoc=parser.parsefromstring(txt,"text/xml"); } else // code ie { xmldoc=new activexobject("microsoft.xmldom"); xmldoc.async=false; xmldoc.loadxml(txt); } return xmldoc; }
Comments
Post a Comment