Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Change XML Attribute with PHP

    Hi,

    I'm trying to use PHP to change the value of an XML element attribute but it's not working. I have an XML file laid out like this:

    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <apple>
      <orange>
        <add key="dog" value="cat"/>
        <add key="fish" value="bird"/>
      </orange>
    </apple>
    I have a HTML form where a users can change the values of the keys. So they input the "key" they want to change, and the new "value" for that key.

    I'm sending the values from the form to the PHP file through JavaScript with this JS code:

    Code:
    function modifyXML(){
    	var key = document.getElementById("modifiedKey").value;
    	var value = document.getElementById("modifiedValue").value;
    
    	var getVar =  "?key=" + key + "?value=" + value;
    	
    	var request = GXmlHttp.create();
    	
    	request.open('GET', 'modifyXML.php' + getVar, true);
    	request.onreadystatechange = function() {
    		if (request.readyState == 4) {
    			window.location.reload();
    		}
    	}
    }
    That part is working. I know that the values are making it into the getVar variable, so the problem must be after that. The PHP file is as follows:

    Code:
    <?php
    header('Content-Type: text/xml;charset=UTF-8');
    
    $key = (float)$_GET['key'];
    $value = (float)$_GET['value'];
    
    $xdoc = new DOMDocument();
    $xdoc->load('config.xml');
    $adds = $xdoc->getElementsByTagName('add');
    
    foreach ($adds as $add) {
           if ($add->getAttribute("key") == $key) {
                   $add->setAttribute("$key", "$value");
                   }
           }
    	   
    $xdoc->save('config.xml');
    ?>
    I can't figure out why it's not working. The request.readyState of the PHP request never reaches 4 (so the page never reloads).

    Anyone have any ideas?

    Thanks very much in advance for any help.

  • #2
    New Coder
    Join Date
    Jun 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Actually, I was just playing around putting an alert at different places in the JavaScript code and I've found out that the JavaScript gets stuck at the line var request = GXmlHttp.create(); (the alert will show up if before that line but not after it). Anyone have any idea why this could be? I have other JavaScripts running on this same server that use that exact same line of code without any problems.

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Alright, silly me. var request = GXmlHttp.create(); wasn't working because GXmlHttp is a google maps thing and I hadn't included the google maps JS file.

  • #4
    New Coder
    Join Date
    Jun 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Still trying to figure out why my PHP file isn't causing any changes in the XML file though....


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •