PHP Argument Passing - Assistance Needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcbrided
Forum Newbie
Posts: 2
Joined: Wed Jul 30, 2008 12:50 pm

PHP Argument Passing - Assistance Needed

Post by mcbrided »

Please bear with me...I've been learning php and attempting to implement it for all of 1 day.

My overall goal with this is to save new or update existing xml files on our web server. This is in a relatively locked down environment in that unless you are working in our lab, there is no network access to this web servers...because of this I am not worried about the security side of how I am implementing the php portion (I'm aware that I can pass any argument and it will hypothetically change files on the server...I am OKAY with this).

This is stictly a proof of concept for this project I am working on...

The quick explanation of what we are doing is creating a web frontend visualizer and management tool for TinyOS/TOSSIM simulations. Because I can't integrate TinyOS with the webserver directly, I'm having to use XML files to transfer information between the two frameworks. Pulling the XML information into the web server is, using javascript, easy and already implemented/working.

Saving files though is not so trivial. We have up to 16 wireless nodes that may get updated at the same time using the same command and value so I wouldn't quite be having a problem if I could update a single node at a time....so instead of expecting:
Managepane.php?node=node5&command=setRFPower&value=18
I may need to consider and expect:
Managepane.php?node=node5&node=node6&node=node7&command=setRFPower&value=18";

Now, I am unsure if this is possible to list multiples of 'node' all in one swoop - so I'm wondering if I can pass a single String of EVERYTHING.

Unless there is an easier way to just 'get this working' I am wanting to send a string of the full XML file as arguments to the php file. I am having major issues getting even portions of this. I've tried using the func_get_args() function to get an array of commands - this doesnt seem to be working. I've also tried to use individual arguments like argv[0],1,2 etc...

Any help would be greatly appreciated. I've commented out stuff within the php but glancing through that it should be pretty obvious what I am trying to accomplish. I have everything EXCEPT getting the correct argument(s) working...

Filename: Managepane.php

Code: Select all

<?php   
    //if (count($argv) > 0) for ($i = 0; i < count($argv); i++) 
    $argList = func_get_args();
    print count($argList);    // count always shows 1
 
    /*
    $xmlstr = $xml;
    if ($xmlstr != "") {
        $dom = new domDocument;
        $dom->loadXML($xmlstr);
        $test = $dom->save("C:/Panorama/Modified.xml");
        echo "Written String :     ".$xmlstr;
        print $xmlstr;
    }
    */
?>
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <!--<title>Untitled Page</title>-->
    <link href="panoramaStyles.css" rel="stylesheet" type="text/css" />   
    <script language="javascript" type="text/javascript">
 
    function init() 
    {
        populateMoteInformation();      
    }        
 
    function populateMoteInformation()
    {
      //load xml file
        var xmlDoc=null;
        
        if (window.ActiveXObject) xmlDoc=new ActiveXObject("Microsoft.XMLDOM");//IE
        else if (document.implementation.createDocument) xmlDoc=document.implementation.createDocument("","",null); //Mozilla
        else alert('Your browser cannot handle this script');
        
        if (xmlDoc!=null)
        {
            xmlDoc.async=false;           
            xmlDoc.load("menu.xml");                
            var x= xmlDoc.getElementsByTagName("Node");
            if(x.length > 0 )
            {
                var nodeList = '<label style="vertical-align: top">Select node(s) : </label><select id="nodes" multiple="multiple" size="4" style="font-family: Tahoma; font-size: 12px; width: 120px">'
                for (i=0;i<x.length;i++)
                { 
                      nodeList = nodeList + '<option value="Node' + x[i].getElementsByTagName("moteID")[0].childNodes[0].nodeValue + '">' 
                      + 'Node ' + x[i].getElementsByTagName("moteID")[0].childNodes[0].nodeValue + '</option>';
                }
                document.getElementById('divNodes').innerHTML = nodeList + '</select>'; 
            }
            
            xmlDoc.load("rpcCommands.xml");                 
            var x= xmlDoc.getElementsByTagName("RpcCommand");
            if(x.length > 0 )
            {
                var rpcCommandList = '<label style="vertical-align: top">Rpc Commands : </label><select id="commands" style="font-family: Tahoma; font-size: 12px; width: 120px">'
                for (i=0;i<x.length;i++)
                { 
                      rpcCommandList = rpcCommandList + '<option value="' + x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue + '">' 
                      + x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue + '</option>';
                }
                document.getElementById('divCommands').innerHTML = rpcCommandList + '</select>';    
            }
        }
    }
    
    function ReturnXMLString() {
      var xml = ""; 
      for (var i = 0; i < document.getElementById("nodes").options.length; i++) {
         var comm = document.getElementById("commands").value;
         var val = document.getElementById("value").value;
         if (document.getElementById("nodes").options[i].selected) {
            var node = document.getElementById("nodes").options[i].value;
 
            xml += "   <" + node + ">\n";
            xml += "      <" + comm + ">" + val + "</" + comm + ">\n";
            xml += "   </" + node + ">\n";
 
         }
      }
      var runPath = "Managepane.php?" + xml;   // actual data to be passed as arguments
 
      document.getElementById("Output").value = runPath;
      //parent.location.href = runPath;
      // lets hardcode some data and see if we can get it working with this...
      parent.location.href = "Managepane.php?node=node5&command=setRFPower&value=18";
    }
    </script> 
 
</head>
<body class="FullSize" onload="init()">    
    <table cellpadding="2" cellspacing="2" style="font-family: Tahoma; font-size: 12px">
        
        <tr>
            <td valign='top' style="height: 100%; width: 100%">
                <div id="divNodes">
                </div>
            </td>
        </tr>
        
        <tr>
            <td valign='top' style="height: 100%; width: 100%">
                <div id="divCommands">
                </div>
            </td>
        </tr>
        
        <tr>
          <td valign='top' style="height: 100%; width: 100%">
            New Value : <input type="text" id="value" name="value" /></td>
        </tr>
        
        <tr>
            <td valign='top' style="height: 100%; width: 100%">
               <input type="submit" value="Modify" onclick='ReturnXMLString();' />
            </td>
        </tr> 
    </table>
    <form><textarea id="Output" style="width:100%" rows="10"></textarea></form>
</body>
</html>
 
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP Argument Passing - Assistance Needed

Post by ghurtado »

mcbrided wrote: Unless there is an easier way to just 'get this working' I am wanting to send a string of the full XML file as arguments to the php file. I am having major issues getting even portions of this.
The problem you will likely run into is that URLs have a practical limit of around 4kb for the lowest common denominator. Given that you would have to URL-encode your XML before putting it into the request URL, that would increase the size of the string another 30% or so, which would make this impossible for any but the smallest XML files.

If you need to transmit large amounts of data to a PHP script, use the post method instead of get.
mcbrided
Forum Newbie
Posts: 2
Joined: Wed Jul 30, 2008 12:50 pm

Re: PHP Argument Passing - Assistance Needed

Post by mcbrided »

So, I could pass an entire needed string using this Post method? Is there an example you can point me to?

While the total xml size would be less than 2KB - I'm not opposed to passing the entire thing using the Post method. This definitely this seems like it would be an easier method since I can format the entire xml exactly how I like it before I even call the php page refresh.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PHP Argument Passing - Assistance Needed

Post by ghurtado »

Here is a great article about how to send a post request form PHP, by one of the top PHP developers:

http://netevil.org/blog/2006/nov/http-p ... thout-curl
Post Reply