Undefined Index

Discussion in 'PHP' started by lost, Dec 19, 2005.

  1. #1
    i'm trying to access the value of some textfields and i keep getting undefined index when i know that the textfield has been created.

    Below is the code...on selection of the 'modifyconfigbutton' i create a number of textfields named "mdes 1", "mdes 2", "mdes 3", "mdes 4", "mdes 5", etc...

    Then on selection of the 'generic_updatebutton', i need to extract the values of each of the textfields. This is where i keep getting undefined index????

    Someone please help!

    
      if(isset($_POST['modifyconfigbutton']))
      {
         for ($index=1; $index<10; $index++)
         echo '<TR><TDDescription <INPUT TYPE="text" NAME="mdes '.$index.'" VALUE="'.$desvalue.'">'</TR>';
    }
    
      if(isset($_POST['generic_updatebutton']))
      {
          for ($i=0;$i<10;$i++)
          {    
              $desValue = $_POST['mdes '+$i+''];
              echo $desValue;
          }
    }
    
    
    PHP:
     
    lost, Dec 19, 2005 IP
  2. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You appear to have a space in the name of the textfields, if this does not solve it click view source and paste the code here so I can look at the form.
     
    dave487, Dec 20, 2005 IP
  3. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes Dave, i do...that is intentional.
     
    lost, Dec 20, 2005 IP
  4. varlik

    varlik Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    lost,
    As Dave says, you have a space in the variable name. You aren't allowed spaces in variable names and I'm pretty sure that applies to post variables too.

    If you need to separate the "mdes" from the number use an underscore instead of the space. You'll have to go back and rename the textfields to match.

    Hope this helps.

    Thomas
     
    varlik, Dec 20, 2005 IP
  5. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    but i've used a space in my variable name before and it works just fine???
     
    lost, Dec 20, 2005 IP
  6. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    here's the source code:
    
    
    <HTML>
    <HEAD>
    <TITLE>System Configuration - Generic</TITLE>
    </HEAD>
    <BODY>
    <CENTER>
    <H1>System Configuration - Generic</H1><TABLE BORDER="0">
    <TR><TD><a title="Home" href="awacswebhome.php">Home</a></TD><TD>|</TD><TD><a title="System Config - Generic" href="/systemconfig-generic.php">System Config - Generic</a></TD><TD>|</TD><TD><a title="System Config - Specific" href="/systemconfig-specific.php">System Config - Specific</a></TD><TD>|</TD><TD><a title="CCA/Assembly" href="../cca-assembly.php">CCA/Assembly</a></TD><TD>|</TD><TD><a title="Tools/Equipment" href="../tools-equipment.php">Tools/Equipment</a></TD><TD>|</TD><TD><a title="Logout" href="./logout.php">Logout</a></TD><TD></TD></TR>
    </TABLE>
    </CENTER>
    <CENTER>
    <TABLE BORDER="0">
    <TR><TD><a title="View Users" href="activeusers.php">Users</a></TD><TD>|</TD><TD><a title="Search Users" href="searchusers.php">Search Users</a></TD>
    
    
    <HTML><HEAD>
    <TITLE>System Configuration - Generic</TITLE>
    </HEAD>
    <BODY>
    <CENTER>
    <TABLE BORDER="0">
        <TD><a title="Add New User" href="addnewuser.php">Add New User</a></TD><TD>
    </TABLE>
    </CENTER>
    <BR>
    <TD> 
    </TABLE>
    </CENTER>
    
    
    <BODY>
    
    <HEAD>  
    <SCRIPT LANGUAGE="Javascript" TYPE="text/javascript">
    
       var arrItems1 = new Array();
       var arrItemsGrp1 = new Array();
       
       arrItems1[0] = "304315-701";
       arrItemsGrp1[0] = 'SDC1-2';
       arrItems1[1] = "304315-702";
       arrItemsGrp1[1] = 'SDC1-2';
       arrItems1[2] = "304315-703";
       arrItemsGrp1[2] = 'SDC1-2';
       arrItems1[3] = "304315-704";
       arrItemsGrp1[3] = 'SDC1-2';
       arrItems1[4] = "304315-705";
       arrItemsGrp1[4] = 'SDC1-2';
       arrItems1[5] = "304315-706";
       arrItemsGrp1[5] = 'SDC1-2';
    
       arrItems1[6] = "304316-701";
       arrItemsGrp1[6] = 'P73 Kit';
       arrItems1[7] = "304316-702";
       arrItemsGrp1[7] = 'P73 Kit';
       arrItems1[8] = "304316-703";
       arrItemsGrp1[8] = 'P73 Kit';
       arrItems1[9] = "304316-704";
       arrItemsGrp1[9] = 'P73 Kit';
    
       arrItems1[10] = "304317-701";
       arrItemsGrp1[10] = 'MDR2-2';
       arrItems1[11] = "304317-702";
       arrItemsGrp1[11] = 'MDR2-2';
    
       arrItems1[12] = "304318-701";
       arrItemsGrp1[12] = 'CSD1-3';
       arrItems1[13] = "304318-702";
       arrItemsGrp1[13] = 'CSD1-3';
       arrItems1[14] = "304318-703";
       arrItemsGrp1[14] = 'CSD1-3';
    
       arrItems1[15] = "304319-701";
       arrItemsGrp1[15] = 'CSD1-4';
       arrItems1[16] = "304319-702";
       arrItemsGrp1[16] = 'CSD1-4';
       arrItems1[17] = "304319-703";
       arrItemsGrp1[17] = 'CSD1-4';
    
       arrItems1[18] = "304320-701";
       arrItemsGrp1[18] = 'MLS1-1';
    
       function selectChange(control, controlToPopulate, ItemArray, GroupArray) 
       {
          var myEle;
          var x;
          var result;
    
          // Empty the second drop down box of any choices
          for (var q = controlToPopulate.options.length; q >= 0; q--) 
          {
              controlToPopulate.options[q] = null;
          }
          // ADD Default Choice - in case there are no values
          myEle   = document.createElement("option");
          theText = document.createTextNode("Select Part #");
          myEle.appendChild(theText);
          myEle.setAttribute("value", "0");
          controlToPopulate.appendChild(myEle);
          // Now loop through the array of individual items 
          // Any containing the same child id are added to
          // the second dropdown box
          for ( x = 0; x < ItemArray.length; x++ ) 
          {
             if ( GroupArray[x] == control.value ) 
             {
                myEle = document.createElement("option");
                myEle.setAttribute(ItemArray[x], x);
                var txt = document.createTextNode(ItemArray[x]);
                myEle.appendChild(txt);
                controlToPopulate.appendChild(myEle);
             }  
          }
      }
    
       function makeDesArray()
      {
         // Array initialization for description fields 
         this[0] = 1;
         for (i = 0; i<1000; i++)
            this[i + 1] = "description 1";
      }
    
      function makePnoArray()
      {
         // Array initialization for partno fields 
         this[0] = 1;
         for (i = 0; i<1000; i++)
            this[i + 1] = "partno 1";
      }
    
      var numItems = 2; 	                    // Number of Items on page
      var desFieldNames = new makeDesArray();   // Array of description textfields
      var pnoFieldNames = new makePnoArray();   // Array of partno textfields
      function addNewItem() 
      {
         desFieldNames[numItems] = 'description '+numItems+'';
         pnoFieldNames[numItems] = 'partno '+numItems+'';
    
         // Add the rows to the Inner HTML of our TD
         document.getElementById("textfields").innerHTML += '<B><FONT SIZE=2>Item '+numItems+' <BR></B>Description&nbsp;&nbsp;<INPUT TYPE="text" NAME="description '+numItems+'" VALUE=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Part Number <INPUT TYPE="text" NAME="partno '+numItems+'" VALUE="" ONKEYDOWN="if (event.keyCode==9) { addNewItem(); }"><BR>';
    
         // Increase the number of items
         numItems++;
         desFieldNames[0] = numItems;
         pnoFieldNames[0] = numItems;
      }
    
      function verifyUpdate()
      {
         msg = confirm("Are you sure you want to update the changes?")
         if (msg)
         {    
         }
      }
    
      function verifySave()
      {
         var descriptionValues = '';
         var partnoValues = '';
    
         msg = confirm("Are you sure you want to save this data?")
         if (msg)
         {    
             var e = document.forms['genericform'].elements;
    
             // loop through the description textfields to retrieve the values
             for (var i = 1; i<desFieldNames[0]; i++) 
             {
                descriptionValues += (e[desFieldNames[i]].value + ',');
                setDescriptionValues(descriptionValues);
                //alert(descriptionValues);
                //alert(e[desFieldNames[i]].value);
             }
             // loop through the partno textfields to retrieve the values
             for (var i = 1; i<pnoFieldNames[0]; i++) 
             {
                partnoValues += (e[pnoFieldNames[i]].value + ',');
                setPartNoValues(partnoValues);  
                //alert(getPartNoValues());
                //alert(e[pnoFieldNames[i]].value);
             }
         }
      }
    
      function setDescriptionValues(desValues)
      {
         descriptionValues = desValues;
         // Set field value
         document.genericform.thedesfield.value = desValues;
      }
    
      function getDescriptionValues()
      {
          return descriptionValues;
      }
    
      function setPartNoValues(pnoValues)
      {
         partnoValues = pnoValues;
         // Set field value
         document.genericform.thepnofield.value = pnoValues;
      }
    
      function getPartNoValues()
      {
          return partnoValues;
      }
    
      function verifyCancel()
      {
         msg = confirm("Are you sure you want to cancel?")
         if (msg)
         {  
            window.location='systemconfig-generic.php';
         }
      }
    
      function showLoad()
      {
         // displays the load button from the hidden state
         document.getElementById('loadData').style.display='';
         //document.getElementById('loadData').onclick=loadData;
         //var index = document.genericform.secondChoice.selectedIndex; 
         //document.genericform.secondChoice.selectedIndex = index;
      }
    
      function displayNewConfig()
      {    
         // hide the unit name selection box
         document.getElementById('firstChoice').style.display='none';
         document.getElementById('firstChoice').value="";
    
         // hide the unit partno selection box
         document.getElementById('secondChoice').style.display='none';
         document.getElementById('secondChoice').value="";
    
         // show the unit name text box for user input and place in focus
         document.getElementById('unitname').style.display='';
         document.getElementById('unitname').focus();    
    
         // show the unit partno textbox for user input
         document.getElementById('unitpartno').style.display='';
    
         // hide the load button
         document.getElementById('loadData').style.display='none';
      }
    
     /* function modifyItems()
      {
         document.getElementById("mtextfield 1").readonly = false;
         document.getElementById("mpartno 1").readonly = false;
      }*/
    
    
    </SCRIPT>
    </HEAD>
    
    <FORM NAME="genericform" ID="genericform" ACTION="systemconfig-generic.php" METHOD="post"/>
      <BR>
      <BR>
      <CENTER>
      <INPUT TYPE=submit NAME="newconfigbutton" DISABLED="disabled" VALUE="Create New Configuration">  
      <INPUT TYPE=submit VALUE="Modify Existing Item" DISABLED="disabled" NAME="modifyconfigbutton">       
      <INPUT TYPE=button VALUE="Search" NAME="genericsearchbutton" ONCLICK="window.location='genericsearchlist.php';">
      </CENTER>
      <BR>
      <BR>
      <!-- create a hidden textboxs for new config items:  unitname, unitpartno  -->
      <B><FONT SIZE=4>Unit Name <INPUT NAME="unitname" TYPE="text" STYLE="display:none;" TABINDEX="1" VALUE=""> 
      <SELECT NAME="firstChoice" ID="firstChoice" ONCHANGE="selectChange(this, genericform.secondChoice, arrItems1, arrItemsGrp1);"> 
      <OPTION VALUE="Select Unit"   selected > Select Unit   </OPTION> 
      <OPTION VALUE="SDC1-2"        SELECTED> SDC1-2  </OPTION>
      <OPTION VALUE="P73 Kit"       > P73 Kit </OPTION>
      <OPTION VALUE="MDR2-2"        > MDR2-2  </OPTION>
      <OPTION VALUE="CSD1-3"        > CSD1-3  </OPTION>
      <OPTION VALUE="CSD1-4"        > CSD1-4  </OPTION>
      <OPTION VALUE="MLS1-1"        > MLS1-1  </OPTION>
      </SELECT></B>
      <!--ONCHANGE='echo '<input TYPE=button NAME="load" ONCLICK=loadData();>'-->
      <BR><B><FONT SIZE=4>Unit Part # <SELECT NAME="secondChoice" ID="secondChoice" >
      <OPTION VALUE="Select"   selected > Select  </OPTION> 
      <OPTION VALUE="---SDC1-2---" >  ---SDC1-2--- </OPTION>
      <OPTION VALUE="304315-701" > 304315-701  </OPTION>
      <OPTION VALUE="304315-702" > 304315-702  </OPTION>
      <OPTION VALUE="304315-703" > 304315-703  </OPTION>
      <OPTION VALUE="304315-704" > 304315-704  </OPTION>
      <OPTION VALUE="304315-705" > 304315-705  </OPTION>
      <OPTION VALUE="304315-706" SELECTED> 304315-706  </OPTION>
      <OPTION VALUE="---P73 Kit---" >  ---P73 Kit--- </OPTION>
      <OPTION VALUE="304316-701" > 304316-701  </OPTION>
      <OPTION VALUE="304316-702" > 304316-702  </OPTION>
      <OPTION VALUE="304316-703" > 304316-703  </OPTION>
      <OPTION VALUE="304316-704" > 304316-704  </OPTION>
      <OPTION VALUE="---MDR2-2---" >  ---MDR2-2--- </OPTION>
      <OPTION VALUE="304317-701" > 304317-701  </OPTION>
      <OPTION VALUE="304317-702" > 304317-702  </OPTION>
      <OPTION VALUE="---CSD1-3---" >  ---CSD1-3--- </OPTION>
      <OPTION VALUE="304318-701" > 304318-701  </OPTION>
      <OPTION VALUE="304318-702" > 304318-702  </OPTION>
      <OPTION VALUE="304318-703" > 304318-703  </OPTION>
      <OPTION VALUE="---CSD1-4---" >  ---CSD1-4--- </OPTION>
      <OPTION VALUE="304319-701" > 304319-701  </OPTION>
      <OPTION VALUE="304319-702" > 304319-702  </OPTION>
      <OPTION VALUE="304319-703" > 304319-703  </OPTION>
      <OPTION VALUE="---MLS1-1---" >  ---MLS1-1--- </OPTION>
      <OPTION VALUE="304320-701" > 304320-701  </OPTION>
      </FONT></SELECT> 
      <INPUT TYPE=submit VALUE="Load" NAME="loadData" ID="loadData"  >
      <INPUT NAME="unitpartno"  TYPE="text" STYLE="display:none;"> 
      <!-- Create a hidden textfield for both the getting description and partno -->
      <INPUT TYPE="hidden" NAME="thedesfield" VALUE="" />
      <INPUT TYPE="hidden" NAME="thepnofield" VALUE="" />
      <!-- Create a hidden textfield for both the modifiedable description and partno textfields -->
      <INPUT TYPE="hidden" NAME="themoddesfield" VALUE="" />
      <INPUT TYPE="hidden" NAME="themodpnofield" VALUE="" />
      <BR>
      <BR>
      <BR>
      <BR>
      <script>document.genericform.newconfigbutton.disabled=false;</script>
    <br />
    <b>Notice</b>:  Undefined offset:  0 in <b>C:\Server\Apache2\htdocs\systemconfig-generic.php</b> on line <b>547</b><br />
    <br />
    <b>Notice</b>:  Undefined offset:  1 in <b>C:\Server\Apache2\htdocs\systemconfig-generic.php</b> on line <b>547</b><br />
    <br />
    <b>Notice</b>:  Undefined offset:  2 in <b>C:\Server\Apache2\htdocs\systemconfig-generic.php</b> on line <b>547</b><br />
    <br />
    <b>Notice</b>:  Undefined offset:  3 in <b>C:\Server\Apache2\htdocs\systemconfig-generic.php</b> on line <b>547</b><br />
    <br />
    <b>Notice</b>:  Undefined offset:  4 in <b>C:\Server\Apache2\htdocs\systemconfig-generic.php</b> on line <b>547</b><br />
    <FONT SIZE=2>The following was updated to the AWACS lab database:<BR>Unit Name: SDC1-2<BR>Unit Part No: 304315-706<BR>  <BR><BR>
      <BR>
      <BR>
      
    </FORM>
    </BODY>
    
    
    <BR>
    <CENTER>
    <IMG SRC="images/gdclogo.gif" ALT="GDC Logo">
    </CENTER>
    </BODY>
    </HTML>
    
    Code (markup):
     
    lost, Dec 20, 2005 IP
  7. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ur variables cant have spaces in them, neither can they start with a numeric value

    if it work for u before is because php is quite forgiving to amateur programmers,

    in C a wrong space could bring ur computer crashing down
     
    cornelius, Dec 21, 2005 IP
  8. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    OK fair enough...i've changed the variable name to have no spaces, it still doesn't work :(
     
    lost, Dec 21, 2005 IP
  9. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    jesus christ man, the first code u posted is full of mistakes, pay close attention to what u code

    BOTH php and html have mistakes, also u have brackets missing here there and everywhere

    try this i cleaned up a few mistakes
    
    <?php
    if (isset ( $_POST['modifyconfigbutton'] ) ){
    	
    	for ($index=1; $index<=10; $index++) {
    		print 	'<tr><td>Description :
    				<input 	type="text" 
    						name="mdes_'.$index.'" 
    						value="'.$desvalue.'" /></td></tr>';
    
        }
    }
    
    
    if( isset( $_POST['generic_updatebutton'] ) ){
    
    	for ($loop=0; $loop<10; $loop++){   
    
    		$post_string = 'mdes_'.$i;
        
    		print $_POST[$post_string].'<br />';
    
    	}
    }
      
    ?>
    
    PHP:
     
    cornelius, Dec 21, 2005 IP