kwstephenchan Posted January 22, 2009 Share Posted January 22, 2009 Hi All, I encountered this "undefined index" error from the following code: for ($i=0;$i<sizeof($arr);$i++) { ?> <tr> <td width='25%'></td> <td width='75%' class='intxt' align='left'> <input type="checkbox" name="box[]" value="<?php echo $darr[$i];?>"><?php echo $narr[$i]; ?></td> </tr> <?php } // closing for loop ?> // when I try to retrieve the value using $_POST['box'], I got the undefined error for ($i=0;$i<count($_POST['box']);$i++) { } Can someone please tell me what have I done wrong? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/ Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 Ummmm post the error next time and more code and please use the [.code] [./code] tags (without the dot) when posting code. Either $darr or $narr does not have an index at $i. Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743394 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 I have no clue as to why your trying to pass data through a checkbox... However, the name of your checkbox is "box[]". Which should mean that in your php the name of your postdata should be "$_POST['box[]']", not "$_POST['box']". Also, in your form, make sure the action attribute is set to "post". As far as th undefined index goes, posting the whole error next time would get you better help quicker. Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743409 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Author Share Posted January 22, 2009 Both $darr and $narr are array with variable elements although same number of elements. Thus, the code is supposed to loop through $arr with matching value from $darr and the text from $narr. The error is: Notice: Undefined index: box .... Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743414 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Author Share Posted January 22, 2009 I have chenged the $_POST from $_POST['box'] to $_POST['box[]'], I get the same error message except this time, the error is: Notice: undefined index: box[] in ....(the program and the line number). As I have never used array of checkbox, I refer to the following example : // This is to collect box array value as global_variables is set off in PHP5 by default $box=$_POST['box']; while (list ($key,$val) = @each ($box)) { echo "$val,"; } echo "<form method=post action=''>"; echo "<table border='0' cellspacing='0' style='border-collapse: collapse' width='100' > <tr bgcolor='#ffffff'> <td width='25%'><input type=checkbox name=box[] value='John'></td> <td width='25%'> John</td> <td width='25%'><input type=checkbox name=box[] value='Mike'></td> <td width='25%'> Mike</td> <td width='25%'><input type=checkbox name=box[] value='Rone'></td> <td width='25%'> Rone</td> </tr> <tr bgcolor='#f1f1f1'> <td width='25%'><input type=checkbox name=box[] value='Mathew'></td> <td width='25%'> Mathew</td> <td width='25%'><input type=checkbox name=box[] value='Reid'></td> <td width='25%'> Reid</td> <td width='25%'><input type=checkbox name=box[] value='Simon'></td> <td width='25%'> Simon</td> </tr> <tr><td colspan =6 align=center><input type=submit value=Select></form></td></tr> </table>"; Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743429 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 Oh, OK, I now see what it is that you are trying to do but I don't believe you can do that in php... The data stored in, $box will always be the last box checked.... I don't know what else to tell you.... Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743473 Share on other sites More sharing options...
sasa Posted January 22, 2009 Share Posted January 22, 2009 change lines for ($i=0;$i<count($_POST['box']);$i++) { } to $count = isset($_POST['box']) ? count($_POST['box']) : 0; for ($i=0;$i<$count;$i++) { } Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743495 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Author Share Posted January 22, 2009 Before I even get into checkimg which box has been checked, I need to resolve the undefined index problem. I have checked letter by letter against examples (that is supposed to work), I just couldn't find what have I done wrong. It is supposed to be simple : put in an input statement: <input style="checkbox" name="box[]" value="ndlkkd"> blah blah blah after that, I need to get those checked box : for each posted box : $_POST['box'] or $_POST['box[]' I just don't get it, simple thing as this should be easy but I am getting the undefined index error while others didn't. There has to be something wrong, just don't know where!!!! Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743511 Share on other sites More sharing options...
DeanWhitehouse Posted January 22, 2009 Share Posted January 22, 2009 However, the name of your checkbox is "box[]". Which should mean that in your php the name of your postdata should be "$_POST['box[]']", not "$_POST['box']". Where the hell did you get that idea from, the name box[] just means the data is added to the array called box, it is not a literal name. Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743513 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 However, the name of your checkbox is "box[]". Which should mean that in your php the name of your postdata should be "$_POST['box[]']", not "$_POST['box']". Where the hell did you get that idea from, the name box[] just means the data is added to the array called box, it is not a literal name. Hm.. I didn't even think about it lol.... That answers a few things.... Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743521 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 Can you place your whole script in a [.code] block for us to analyze please? Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743524 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Author Share Posted January 22, 2009 I changed the style from checkbox to text and get identical problem: <input style="text" name="boxt[]" value="blah blah"> blah blah blah When I tried to get it: $_POST['boxt'] I get the undefined index: boxt in ... I'm lost!!! Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743530 Share on other sites More sharing options...
DeanWhitehouse Posted January 22, 2009 Share Posted January 22, 2009 to change a type from checkbox to text it would be <input type[b/]="text"> not style Also the checkbox is actually within the form tags yes? Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743533 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Author Share Posted January 22, 2009 Sorry for the typo, in the script, it was type not style. The following are the script. Sorry, blobenny, I don't know what you mean by [.code], so I just stick it here.. **** <form action="<?php echo $editFormAction; ?>" method="POST" name="form1" style="margin:0px;" > <table width="100%" border="0" cellspacing="0" cellpadding="1"> <?php // table 4 ?> <tr valign="baseline"> <td width="30%" height="20" align="left" class="lbtxt">Select Device : </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <?php for ($i=0;$i<sizeof($darr);$i++) { ?> <tr> <td width='25%'></td> <td width='75%' class='intxt' align='left'> <input type="checkbox" name="box[]" value="<?php echo $darr[$i];?>"><?php echo $narr[$i]; ?></td> </tr> <?php } // closing for loop ?> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr valign="baseline"> <td width="25%" height="20" align="right" class="lbtxt">Selection Criteria : </td> <td width="75%" height="20" class="intxt"> <input name="m_status" type="radio" value="a" checked> Available <input type="radio" name="m_status" value="p"> Pending</td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr valign="baseline" class="wntxt"> <td colspan=2 height="20" class="wntxt"> <?php echo "Note : ".$errmsg; ?></td> </tr> </table> <hr size="1" noshade class="whiteBox"> <?php // if (empty($errmsg)) { ?> <input type="submit" name="Submit" value="Go"> <?php // } ?> <input type="button" name="Submit" value="Return" onClick="window.history.back();"> <input type="button" name="Submit" value="Cancel" onClick="window.location='main-menu.php'"> <input type="hidden" name="MM_insert" value="form1"> </form></td> **** after this - to retrieve the value ** if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { for ($i=0;$i<count($_POST['box[]']);$i++) { } } hope it is not too messy!! Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743568 Share on other sites More sharing options...
kwstephenchan Posted January 22, 2009 Author Share Posted January 22, 2009 BTW, I have changed the $_POST['box[]']; back to $_POST['box']; Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743573 Share on other sites More sharing options...
kwstephenchan Posted January 23, 2009 Author Share Posted January 23, 2009 my version of php is 4.3.1 is there anything to do with the problem?? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743890 Share on other sites More sharing options...
landavia Posted January 23, 2009 Share Posted January 23, 2009 <form action="<?php echo $editFormAction; ?>" method="POST" name="form1" style="margin:0px;" > <table width="100%" border="0" cellspacing="0" cellpadding="1"> <?php // table 4 ?> <tr valign="baseline"> <td width="30%" height="20" align="left" class="lbtxt">Select Device : </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <?php for ($i=0;$i<sizeof($darr);$i++) { ?> <tr> <td width='25%'></td> <td width='75%' class='intxt' align='left'> <input type="checkbox" name="box[]" value="<?php echo $darr[$i];?>"><?php echo $narr[$i]; ?></td> </tr> <?php } // closing for loop ?> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr valign="baseline"> <td width="25%" height="20" align="right" class="lbtxt">Selection Criteria : </td> <td width="75%" height="20" class="intxt"> <input name="m_status" type="radio" value="a" checked> Available <input type="radio" name="m_status" value="p"> Pending</td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr valign="baseline" class="wntxt"> <td colspan=2 height="20" class="wntxt"> <?php echo "Note : ".$errmsg; ?></td> </tr> </table> <hr size="1" noshade class="whiteBox"> <?php // if (empty($errmsg)) { ?> <input type="submit" name="Submit" value="Go"> <?php // } ?> <input type="button" name="Submit" value="Return" onClick="window.history.back();"> <input type="button" name="Submit" value="Cancel" onClick="window.location='main-menu.php'"> <input type="hidden" name="MM_insert" value="form1"> </form></td> here goes the neighood if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { for ($i=0;$i<count($_POST['box']);$i++) { } } why use count? use sizeof() Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743896 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 why use count? use sizeof() count() is actually faster than sizeof() sizeof() is considered the alias of count() and not the other way around. I'd personally use count() Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743899 Share on other sites More sharing options...
landavia Posted January 23, 2009 Share Posted January 23, 2009 ........... I'd personally use count() thx.. buat how about this is there 2 dimension array $box=array(array(1,3,4),array(5,6,1)); count($box) => .... sizeof($box) => ..... hm both same? <form action="<?php echo $editFormAction; ?>" method="POST" name="form1" style="margin:0px;" > <table width="100%" border="0" cellspacing="0" cellpadding="1"> <?php // table 4 ?> <tr valign="baseline"> <td width="30%" height="20" align="left" class="lbtxt">Select Device : </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <?php $darr=array(1,3,4,2,6,8,12); for ($i=0;$i<sizeof($darr);$i++) { ?> <tr> <td width='25%'></td> <td width='75%' class='intxt' align='left'> <input type="checkbox" name="box[]" value="<?php echo $darr[$i];?>"><?php echo $darr[$i]; ?></td> </tr> <?php } // closing for loop ?> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr valign="baseline"> <td width="25%" height="20" align="right" class="lbtxt">Selection Criteria : </td> <td width="75%" height="20" class="intxt"> <input name="m_status" type="radio" value="a" checked> Available <input type="radio" name="m_status" value="p"> Pending</td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr valign="baseline" class="wntxt"> <td colspan=2 height="20" class="wntxt"> <?php echo "Note : ".$errmsg; ?></td> </tr> </table> <hr size="1" noshade class="whiteBox"> <?php // if (empty($errmsg)) { ?> <input type="submit" name="Submit" value="Go"> <?php // } ?> <input type="button" name="Submit" value="Return" onClick="window.history.back();"> <input type="button" name="Submit" value="Cancel" onClick="window.location='main-menu.php'"> <input type="hidden" name="MM_insert" value="form1"> </form></td></table> <?php if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { for ($i=0;$i<count($_POST['box']);$i++) { print $_POST['box'][$i]."<br/>"; } } print_r($_POST); ?> huh.. that's strange.. work on my komp Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743929 Share on other sites More sharing options...
kwstephenchan Posted January 23, 2009 Author Share Posted January 23, 2009 landavia, can you tell me What is your version of php ?? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-743980 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 ........... I'd personally use count() thanks.. buat how about this is there 2 dimension array $box=array(array(1,3,4),array(5,6,1)); count($box) => .... sizeof($box) => ..... hm both same? Did you actually try that code? They both return the integer 2. And yes they both are the same. This is why sizeof() doesn't have it's own manual on php.net but a link to count(). Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-744150 Share on other sites More sharing options...
landavia Posted January 25, 2009 Share Posted January 25, 2009 >>can you tell me What is your version of php ?? PHP Version 5.1.6 Did you actually try that code? They both return the integer 2. And yes they both are the same. This is why sizeof() doesn't have it's own manual on php.net but a link to count().my mistake.. after read and try it out.. i found out that's function have same result btw.. let's solve the TS problem Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-745851 Share on other sites More sharing options...
kwstephenchan Posted January 25, 2009 Author Share Posted January 25, 2009 I insert the same codes in a different machine which has a higher version of PHP and it works. Thanks to all. Quote Link to comment https://forums.phpfreaks.com/topic/141973-solved-undefined-index-error-of-what-is-meant-to-be-an-array-of-checkbox/#findComment-745926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.