472,121 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

How do I update data within an xml childnode using XMLHttpRequest

OK, I'm pretty sure this cannot work because I'm trying to use JavaScript (client-side) to write to an xml file (which is server-side) using XMLHttpRequest.

Can I use PHP do what I'm trying to do?

Here's my code:

The function mySaveFunction() is called by clicking the "Update" button, after the user changes the data which is populated in the form fields, which was retrieved via XMLHttpRequest from an XML external file.

I know that XMLHttpRequest can createElement() but can it somehow update the text within the node?

Please help and thanks in advance

Expand|Select|Wrap|Line Numbers
  1.  
  2.     function mySaveFunction()
  3.     {
  4.         try
  5.         {
  6.             if (window.ActiveXObject)
  7.             {
  8.             logovar[0].childNodes[0].InnerText = document.getElementById('logotextfield').value;
  9.             headingvar[0].childNodes[0].InnerText = document.getElementById('headertextfield').value;
  10.             contentvar[0].childNodes[0].InnerText = document.getElementById('textareafield').value;
  11.             }
  12.             else if(window.XMLHttpRequest)
  13.             {
  14.             logovar[0].childNodes[0].InnerText = document.getElementById('logotextfield').value;
  15.             headingvar[0].childNodes[0].InnerText = document.getElementById('headertextfield').value;
  16.             contentvar[0].childNodes[0].InnerText = document.getElementById('textareafield').value;
  17.             data1 = logovar[0].childNodes[0].InnerText;
  18.             data2 = headingvar[0].childNodes[0].InnerText;
  19.             data3 = contentvar[0].childNodes[0].InnerText;
  20.             XMLHttpRequest.send(data1);
  21.             XMLHttpRequest.send(data2);
  22.             XMLHttpRequest.send(data3);
  23.             } 
  24.             else 
  25.             {
  26.             logovar[0].childNodes[0].InnerText = document.getElementById('logotextfield').value;
  27.             headingvar[0].childNodes[0].InnerText = document.getElementById('headertextfield').value;
  28.             contentvar[0].childNodes[0].InnerText = document.getElementById('textareafield').value;
  29.             }    
  30.         catch(e)
  31.         {
  32.             alert(errorHappendHere);
  33.         }        
  34.     }
  35.  
  36.  
  37.  
  38.     try
  39.     {
  40.         if (window.ActiveXObject)
  41.         {
  42.             var errorHappendHere = "Check Browser and security settings";
  43.             xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  44.             xmlDoc.async=false;
  45.             xmlDoc.load('xml/badgirls_casting2.xml');
  46.         }
  47.         else if(window.XMLHttpRequest)
  48.         {
  49.             var errorHappendHere = "Error handling XMLHttpRequest request";
  50.             var d = new XMLHttpRequest();
  51.             d.open("GET", 'xml/badgirls_casting2.xml', false);
  52.             d.send(null);
  53.             xmlDoc=d.responseXML;
  54.         } else {
  55.             var errorHappendHere = "Error.";
  56.             xmlDoc = document.implementation.createDocument("","",null);
  57.             xmlDoc.async=false;
  58.             xmlDoc.load('xml/badgirls_casting2.xml');
  59.         }
  60.     }    
  61.     catch(e)
  62.     {
  63.         alert(errorHappendHere);
  64.     }
  65. var logovar=xmlDoc.getElementsByTagName('logo');
  66. var headingvar=xmlDoc.getElementsByTagName('heading');
  67. var contentvar=xmlDoc.getElementsByTagName('content');
  68.  
  69. document.writeln('<table><tr><td>');
  70. document.writeln("Image: </td><td>");
  71. document.writeln("<input type='textfield' style='position:relative; width:200px;' id='logotextfield' value='" + logovar[0].childNodes[0].nodeValue + "'>");
  72. document.writeln("</td></tr><tr><td>");
  73. document.writeln("Header:</td><td>"); 
  74. document.writeln("<input type='textfield' style='position:relative; width:200px;' id='headertextfield' value='" + headingvar[0].childNodes[0].nodeValue + "'>");
  75. document.writeln("</td></tr><tr><td>");
  76. document.writeln("Content: </td><td>");
  77. for (i=0;i<contentvar.length;i++)
  78.   {
  79.   if (contentvar[i].nodeType==1)
  80.     { 
  81.         document.writeln("<textarea cols='30' rows='30' wrap='hard' id='textareafield["+i+"]'>" + contentvar[i].childNodes[0].nodeValue + "</textarea>");
  82.     }
  83.   }
  84. document.writeln('</td></tr><tr><td colspan="2">');
  85. document.writeln('<input type=button onClick=javascript:mySaveFunction(); value=Update>');
  86. document.writeln('</td></tr></table>');
  87.  
  88.  
May 31 '07 #1
1 5201
OK, I've found a tutorial on XPath -- http://www.w3schools.com/xquery/xquery_select.asp and the following is what I have changed in my code.

It seems pretty straight forward but it's not working :-(

I have placed this code in the header section of the HTML file. The JavaScript is called when the user clicks on the button "Update" after modifying the fields in the form, for which the fields were retrieved from an external XML file using XMLHttpRequest.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     <script language="javascript">
  3.     <!--
  4.     function mySaveFunction()
  5.     {
  6.             var errorHappendHere = "Error handling XMLHttpRequest request";
  7.             var d = new XMLHttpRequest();
  8.             d.open("POST", 'xml/badgirls_casting2.xml', true);
  9.             d.send(return <logo> document.getElementById('logotextfield').value; </logo>);
  10.             d.send(return <header> document.getElementById('headertextfield').value; </header>);
  11.             d.send(return <content> document.getElementById('textareafield').value; </content>);
  12.             xmlDoc=d.responseXML;
  13.     }
  14.     //-->
  15.     </script>
  16.  
  17.  
  18.  
Is it not working because the Javascript which writes the two textfields and one textarea, is within a div?

Expand|Select|Wrap|Line Numbers
  1.  
  2.         <div>
  3.             <script language="javascript" src="js_files/badgirls_casting_module.js"></script>
  4.         </div>
  5.  
  6.  
Which produces the code beginning with try -- above?

Is it because the file is already opened to populate the textfields and textarea and therefore cannot be altered?

Shouldn't this fill the nodes of the XML document with the values that have been altered in the respective textfields and textarea with no problem?

Thanx in advance :-)
May 31 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Stephen Plotnick | last post: by
1 post views Thread by blintrell | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.