473,379 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 5279
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

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

Similar topics

5
by: Jarson | last post by:
My JavaScript is trying to POST data to a CGI script (Perl) using XMLHttpRequest. My CGI server gets different data from IE than Mozilla Firefox. // For Mozilla, req = new XMLHttpRequest(); //...
4
by: William | last post by:
My first time posting to clj , I have already read http://jibbering.com/faq/#FAQ2_3 before posting. javascript function in question: saveText() is as follows: function saveText(...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
7
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it...
7
by: John J. Hughes II | last post by:
I have a DataGridView with a TextBoxColumn. I setting the data source to a List<stringvalue in a static class. The list is filled from a background thread. So far all is fine and it works...
4
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You...
2
by: ajaxcoder | last post by:
Hi In my project i had a login form and i am trying to send the username and password to the server for authentication using xmlHttpRequest. Hence i am using POST request but i am unable to send...
1
by: blintrell | last post by:
<?xml version="1.0" encoding="utf-8"?> <root> <groups> <group gid="1" name="CSM"> <contact cid="1" /> <contact cid="2" /> <contact cid="3" /> <contact cid="4" /> ...
0
by: GavReynolds | last post by:
Hi All I am writing a program to load in an xml file from the server and create an animation to represent this data (for use as a 'front end' for the data). I have loaded in the XML data into...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.