PHP Array not functioning anymore

Discussion in 'PHP' started by cbkman, Jul 6, 2009.

  1. #1
    Hello all!

    I've put together a script so that users at my work location can request and checkout equipment. One of the pieces of the script on the first page has "server os" and "server os version". A user can click a button that uses javascript to clone that aspect of the form so the user can add multiple servers. On the second page I'd put together the following code:

    <?php
    $sosv = $_POST['serverostype'];
    $x = $_POST['serverosversion'];
    {
    foreach($_POST[serverostype] as $sosv)
    	$sa = array_combine($_POST['serverostype'], $_POST['serverosversion']);
    foreach ($sa as $v1 => $v2)
    	{
    	echo "<br>";
    	echo 'Server OS type: <b>'.$v1.'</b><br>';
    	echo 'Server OS version: <b>'.$v2.'</b><br>';
    	}
    }
    ?>
    PHP:
    Initially this code had worked, for weeks it had been functioning fine. After a few weeks of getting sidetracked with other projects at my job I'd gone back to work on this script, and now I'm getting the following error whenever submitting the form:

    The odd thing is I am positive that this script worked flawlessly before, and now the script wont work, I havent changed the code at all since it was initially working, the only thing thats different is that the system this script is running on just recently got updated with SP2 and patched with some windows security patches. I am puzzled as to why this isn't functioning.

    Is the way I went about coding the array wrong? If anyone can help me out I'd greatly appreciate it!
     
    cbkman, Jul 6, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Debug what's in those arrays. Usually you cant pass arrays through an input variable unless you serialize it.

    Try:
    
    <?php
    
    echo "<pre>";
    print_r($_POST['serverostype']);
    print_r($_POST['serverosversion']);
    echo "</pre>";
    
    $sosv = $_POST['serverostype'];
    $x = $_POST['serverosversion'];
    {
    foreach($_POST[serverostype] as $sosv)
        $sa = array_combine($_POST['serverostype'], $_POST['serverosversion']);
    foreach ($sa as $v1 => $v2)
        {
        echo "<br>";
        echo 'Server OS type: <b>'.$v1.'</b><br>';
        echo 'Server OS version: <b>'.$v2.'</b><br>';
        }
    }
    ?>
    
    PHP:
     
    jestep, Jul 6, 2009 IP
  3. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Oddly if I submit the form without selecting anything from my dropdown menus it posts through echoing both the serveros and the serverosversion

    (the first option on each dropdown just says to select an operating system / version)

    If I go ahead and select an operating system and version I get the following:

    So the first oddity is that
    1) when leaving it default, its able to recognize both variables and is able to echo them, but once I select options on the dropdown, its no longer able to echo the second variable, only the first..
    2) When I go to add more sets of operating systems / versions even putting two sets and submitting the form, it will only echo one set, when it should be showing two different sets on the second page.
     
    cbkman, Jul 6, 2009 IP
  4. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey just 2 quick observations, shouldn't:

    foreach($_POST[serverostype] as $sosv)
    be
    foreach($_POST['serverostype'] as $sosv)

    and shouldn't this foreach loop be have curly braces in it.

    Anyway it seems from the error that something is causing $_POST['serverosversion'] to equal NULL and not an array, so maybe it's something to do with the form, in particular the serveros column
     
    wd_2k6, Jul 6, 2009 IP
  5. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ahh yes I should have the single quotes in there, but even after adding them It still produces the same results.

    also, totally forgot to mention that the results Ive explained are all occuring in FireFox, if I use internet explorer and make selections on the dropdown menu, the print out fine on the second page, only with firefox am I not able to get the results of the second variable to print out.

    Ive tried running this on multiple systems here at my workplace and they all produce the same results, all with different versions of IE /Firefox, IE echoes what it should, where firefox is losing the second variable.
     
    cbkman, Jul 6, 2009 IP
  6. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think it's a problem with the form then cause if php is server-side then results shouldnt differ from browser to browser.
     
    wd_2k6, Jul 6, 2009 IP
  7. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If it had to do with the form, why would it work one day and then not the next? Is it possible that updating the system to SP2 could affect the way my codes processed? It doesnt sound reasonable to me but I thought I'd ask.. my forms not anything crazy,

    I have my first dropdown "serverostype[]" where you can pick from Windows, solaris, HP/UX, AIX.. and once you select an option I use ajax to populate the second dropdown "serverosversion[]" with versions of the operating system that apply per OS...


    Heres an example of what the code looks like live in the browser


    Heres what the code looks like on the backend

    <?php
    //connnects to mssql
    $con = mssql_connect("localhost","xxx","xxx");
    mssql_select_db('CSSLAB', $con);
    $query="select DISTINCT platform from OS WHERE status='Active' and type='server' ORDER by platform desc";
    $result = mssql_query($query);
    ?>
    <div class="overFlowAdds">
    <span id="set1" class="inputs">
    <span class="formInputs" id="set1Input1">
    <table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="120">Operating System </td>
        <td width="120">
    	<select name="serverostype[]" onChange="getOSV('findcity.php?serverostype='+this.value,0)">
    	<option value="Select Server OS">Select Server OS</option>
    	<?php while($row=mssql_fetch_array($result)) { ?>
    	<option value="<?php echo $row['platform'] ?>"><?php echo $row['platform'] ?></option>
    	<?php } ?>
     </select></td>
        <td>OS Version&nbsp;</td>
        <td><div id="osdiv0">
    <select name="serverosversion[]">
    	<option value="Select OS Version">Select OS Version</option>
            </select></div></td>
      </tr>
     <a href="#" class="add"><img src="./addBtn.gif" alt="add" /></a>
     </table>
    </span>
    </span>
    </div>
    PHP:
    and this is the content of findcity.php (where the second dropdown gets its data populated from)

    <?php
    if (isset($_GET['serverostype']))
    {
    $serverostype = $_GET['serverostype'];
    $ostype = server;
    }
    else
    {
    $serverostype = $_GET['clientostype'];
    $ostype = client;
    }
    //connnects to the mssql
    include("../home/functions.php");
    
    //connnects to mssql
    $con = mssql_connect("localhost","uiuser","uiuser");
    mssql_select_db('CSSLAB', $con);
    $query="select * from OS WHERE platform='".$serverostype."' and status='Active' and type='".$ostype."' order by version desc";
    $result = mssql_query($query);
    
    ?>
    <select name="<?php echo $ostype; ?>osversion[]"><option>Select Os Version</option><?php while($row=mssql_fetch_array($result)) { ?><option value="<?php echo $row['version'].' '.$row['OSlevel'] ?>"><?php echo $row['version'].' '.$row['OSlevel'] ?></option><?php } ?></select>
    <br><br>
    
    
    
    
    
    
    PHP:

    Thanks for the quick replies!
     
    cbkman, Jul 6, 2009 IP
  8. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #8
    are those, serverostype and serverosversion selector multiple selectors?
    Why are you using [] in the names of these selectors when you are creating only one of each.

    on findcity.php you have this code:
    <select name="<?php echo $ostype; ?>osversion[]">
    <option>Select Os Version</option><?php while($row=mssql_fetch_array($result)) { ?>
    <option value="<?php echo $row['version'].' '.$row['OSlevel'] ?>"><?php echo $row['version'].' '.$row['OSlevel'] ?></option>
    <?php } ?></select>
    PHP:
    I am guessing this selector replaces the one (i.e. <select name="serverosversion[]">) through javascript.

    This explains why your 2nd array is null as soon as you select the server OS.
    as soon as you choose server OS, you remove the selector 'serverosversion' from your form and replace it with a selector named '<?php echo $ostype; ?>osversion[]';

    what you need to do is replace only the <options> in the already existing selector 'serverosversion[]'. Not remove it and add another selector.
     
    samyak, Jul 6, 2009 IP
  9. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Samyak, thanks for the reply.

    I use [] in the names because on my form I'm using javascript to duplicate the fields "serverostype" and "serverosversion". I dont know how many servers my users will want to checkout therefore I let them duplicate the fields however many times. my duplication process is done using:

    <script type="text/javascript">
    $(function(){ addDuplicateFormFuncs(); });
    
    function addDuplicateFormFuncs(){
    
    	$(".inputs").each(function(i){//for each set of .inputs being set1, set2, set3 etc...
    		
    		repeat(this.id); // get the current id of the .inputs element and pass it to the repeat function
    		
    		function repeat(currentSet){
    	
    			var totalInputs = $("#" + currentSet + " .formInputs").length;//get current length of .formInputs of the current set ex. = #set1 .formInputs
    
    			$("#" + currentSet + " .formInputs:last .add:visible").click(function(){//add click handler to current sets .add ex. = #set1Input1 .add
    				
    				$("#" + currentSet + " .formInputs:last .add").css({"visibility":"hidden"});//hide button for current sets .add
    				$("#" + currentSet + " .formInputs:last .add").unbind();//remove click event from the prior input
    			var divcount = currentSet+"Input"+($("#" + currentSet + " .formInputs").length + 1);
    				$("#" + currentSet + " .formInputs:last").after("<?php
    					//connnects to mssql
    					$con = mssql_connect("localhost","uiuser","uiuser");
    					mssql_select_db('CSSLAB', $con);
    					$query="select DISTINCT platform from OS WHERE status='Active' ORDER by platform desc";
    					$result = mssql_query($query);
    					?><span class=\"formInputs\" id='"+currentSet+"Input"+($("#" + currentSet + " .formInputs").length + 1)+"'><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>Operating System&nbsp;&nbsp;</td><td><select name=\"serverostype[]\" onChange=\"getOSV('findcity.php?serverostype='+this.value,'"+divcount+"')\"> <option value=''>Select Server OS</option><?php while($row=mssql_fetch_array($result)) { ?><option value='<?php echo $row['platform'] ?>'><?php echo $row['platform'] ?></option><?php } ?></select></td><td>&nbsp;OS Version&nbsp;</td><td><div id='osdiv"+divcount+"'><select name='serverosversion[]'><option>Select OS Version</option></select></div></td></tr>&nbsp; <a href='#' class='remove'><img src='./removeBtn.gif' alt='remove' /></a>&nbsp;<a href='#' class='add'><img src='./addBtn.gif' alt='add' /></a></table></form></span></span></div>")// add another set of inputs for a specific set				
    				if ($("#"+currentSet).parent()[0].scrollHeight != 700){//scroll box 
    					$("#"+currentSet).parent()[0].scrollTop = $("#"+currentSet).parent()[0].scrollHeight;
    				}				
    				$("#" + currentSet + " .formInputs").removeClass("oddBg");//remove oddBg class
    				$("#" + currentSet + " .formInputs:odd").addClass("oddBg");//add back in oddBg class				
    				$("#"+currentSet+" .formInputs input").each(function(){//update the name attribute of the new Input added
    					this.name = "";
    					var inputName = ($(this).parent().parent().attr('id')) + this.alt;
    					this.name = inputName + counter;
    					//this.value = inputName;
    				})		
    				$("#"+currentSet+"Input"+ (totalInputs + 1) +" .remove").click(function(){//bind click event to the remove button	
    					if($(this).parent().next().length == 0){//if there are no inputs after these inputs then you are the last one so...
    					$(this).parent().prev().children(".add").css({"visibility":"visible"});//naviagte to the prior inputs and turn on the add 
    					}					
    					$(this).parent().remove();//remove this inputs for a specific set					
    					$("#" + currentSet + " .formInputs").removeClass("oddBg");//remove oddBg class
    					$("#" + currentSet + " .formInputs:odd").addClass("oddBg");//add back in oddBg class				
    					$("#" + currentSet + " .formInputs").each(function(i){
    						this.id = currentSet + "Input" + (i + 1);
    						$("#"+currentSet+" .formInputs input").each(function(){//update the name attribute of the new Input added
    						this.name = "";
    						var inputName = ($(this).parent().parent().attr('id')) + this.alt;
    						this.name = inputName;
    						//this.value = inputName;
    				})
    					})					
    					repeat(currentSet); //run the repeat function again on the new instance of the DOM, with elements removed					
    					return false; //stop browser defalut event of href
    				})
    								repeat(currentSet); //run this function again on the new instance of the DOM, with elements added
    						return false;//stop browser defalut event of href		
    			})//end click handler			
    		}//end repeat function		
    	})//end each for .inputs
    }
    </script>
    Code (markup):

    I'm using <select name="<?php echo $ostype; ?>osversion[]">
    because findcity.php is called for two seperate functions, both "servers" and "clients"

    so basically by default theres two sets of dropdowns...
    "serverostype","serverosversion" as well as "clientostype" and "clientosversion" .

    When a user selects either a "server os" or a "client os" the findcity.php script (up at the top) figures out if im looking for a client or a server, and in doing so pushes back to the other page the dropdown populated with certain options based on the selection of either server or a client.
     
    cbkman, Jul 6, 2009 IP
  10. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Just wanted to share with you what the source looks like after a selection is made for the os and os version

    <table cellspacing="0" cellpadding="0" border="0">
    <tbody>
    <tr>
    <td width="120">Operating System </td>
    <td width="120">
    <select onchange="getOSV('findcity.php?serverostype='+this.value,0)" name="serverostype[]">
    <option value="Select Server OS">Select Server OS</option>
    <option value="Windows ">Windows </option>
    <option value="Solaris">Solaris</option>
    <option value="HP/UX">HP/UX</option>
    <option value="AIX">AIX</option>
    </select>
    </td>
    <td>OS Version </td>
    <td>
    <div id="osdiv0">
    <select name="serverosversion[]">
    <option>Select Os Version</option>
    <option value="2008 Server Std.">2008 Server Std.</option>
    <option value="2008 Server Ent. Edition">2008 Server Ent. Edition</option>
    <option value="2003 Server Std. ">2003 Server Std. </option>
    <option value="2003 Server Ent. Edition ">2003 Server Ent. Edition </option>
    <option value="2000 Server">2000 Server</option>
    <option value="2000 Advanced Server ">2000 Advanced Server </option>
    </select>
    <br/>
    <br/>
    </div>
    </td>
    </tr>
    </tbody>
    Code (markup):

    To me that form looks completely fine, and in posting that to the second page I'm still not getting the second variable to appear.
     
    cbkman, Jul 6, 2009 IP
  11. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #11
    So what happens if you add

    
    echo $_POST['serverosversion']; 
    PHP:
    at the top of your php then go through the form select an ostype and version and submit, what does it print out, the word "Array" or a value or an error.
     
    wd_2k6, Jul 7, 2009 IP
  12. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    after putting the
    echo $_POST['serverosversion'];
    PHP:
    line at the top and selecting an operating system/version It did not echo anything back.
     
    cbkman, Jul 8, 2009 IP
  13. cbkman

    cbkman Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I still haven't gotten this resolved..

    In doing more troubleshooting with firefox Ive come to realize that the second variable isnt even getting passed to the headers at all.
     
    cbkman, Jul 13, 2009 IP