Jump to content

[SOLVED] undefined index error of what is meant to be an array of checkbox


kwstephenchan

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!!!!

Link to comment
Share on other sites

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.... :(

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

<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()

Link to comment
Share on other sites

...........

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

Link to comment
Share on other sites

...........

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().

Link to comment
Share on other sites

>>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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.