469,592 Members | 2,032 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

Unable to pull $_POST for checkbox?

Tarantulus
114 100+
Hi,

ok, quick description of the problem, I'm trying to reference the postdata from some checkboxes. Unfortunately the checkboxes are dynamically generated therefore I don't know how many there are and thus can't name them individually.

I remember reading that naming them with square brackets (as "name[]") will automatically create an array in the postdata (meaning $_POST[name] is an array) is this true?

anyway, the fact is that my checkboxes are not posting. I print_r($_POST) and it comes back with all expected posts BUT not the checkbox postdata, even if I don't name them with [].

I will post code here for reference, but it's quite long.

Expand|Select|Wrap|Line Numbers
  1. <?PHP
  2. //$visible=array();
  3. echo("<LINK REL=StyleSheet HREF=\"stylesheet.css\" TYPE=\"text/css\">");
  4.  
  5. function push(&$array, &$object){    
  6.     $array[] =& $object;    
  7. function CleanA($array) {
  8. while ($count < count($arr)) {
  9. if ($arr[$count] == "") {
  10. unset($arr[$count]);
  11. }
  12. $count++;
  13. }
  14. return $arr;
  15.  
  16.  
  17. echo("<BODY scroll=\"auto\"><script type=\"text/javascript\">\nfunction changeText(val){\nvar txtArea = document.getElementById('desc');\ntxtArea.value = val;\n}\n</script>");
  18.  
  19. $datetoday=date("d/m/y");
  20. include("dbconnect.php");
  21.     db_connect();
  22.  
  23.  
  24.     echo("<BR><center><FORM name=main action=$PHP_SELF?TID=$TID method=\"post\" >");
  25.     echo("<SELECT name=\"related\"  onChange=this.form.submit()><OPTION></OPTION>\n");
  26.     $stkgrpqueryedit="SELECT DISTINCT id, service FROM serv_resp_service ORDER BY service ASC";
  27.     $stkgrpsqledit=mysql_query($stkgrpqueryedit)or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $stkgrpqueryedit");
  28.  
  29.     $i=0;
  30.     $lenedit=mysql_numrows($stkgrpsqledit);
  31.  
  32.     while ($i<$lenedit){
  33.         $stkgrpname=mysql_result($stkgrpsqledit,$i,"service")or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $stkgrpqueryedit");
  34.         $stkgrpid=mysql_result($stkgrpsqledit,$i,"id")or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $stkgrpqueryedit");
  35.  
  36.         echo("<OPTION>$stkgrpname</OPTION>\n");
  37.  
  38.         $i++;
  39.  
  40.     };
  41.     echo("</SELECT>");
  42.  
  43.     $hidden="$existing,$_POST[hidden]";
  44.     $hidden=ereg_replace(" ","_",$hidden);
  45.     $visible=explode(",",$hidden);
  46.     $_POST[related]=ereg_replace(" ","_",$_POST[related]);
  47.     push($visible,$_POST[related]);            
  48.         if(isset($_POST[remove])){
  49.         unset($visible[$_POST[remove]]);
  50.  
  51.      }
  52.          $run=count($visible);
  53.     $m=0;
  54.     echo("<BR><TABLE>");
  55.      while ($m<$run){
  56.  
  57.  
  58. //         $visquery="SELECT name from stk_groups WHERE id=$visible[$m]";
  59. //         $visrun=mysql_query($visquery);
  60. //         $visresult=mysql_result($visrun,0)or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $visquery");
  61.         if($visible[$m]!=""){
  62.         echo("\n<TR><TD class=darker align=left>$visible[$m]</TD><TD class=darker align=centre><a href=\"#\" ><img src=\"http://tmc-tectest/fileadmin/open_resources/image_library/icons/icon_delete.gif\" border=0 alt=\"$m\" onmousedown=\"changeText(this.alt)\" onmouseup=main.submit()></a></TD><TD class=darker align=right><input type=\"checkbox\" value=\"$visible[$m]\" name=\"deleted[]\"></input></TD></TR>");
  63.     }$m++;
  64. }
  65.     if($m=$run){
  66.          echo("</TABLE>");
  67.         }
  68.  
  69.      foreach($visible as $key => $value) { 
  70.   if($value == "") { 
  71.     unset($visible[$key]); 
  72.   } 
  73. $visible = array_values($visible); 
  74.     if($op=="upload"){
  75. //     insertSQLhere
  76.         }
  77.     $visible=implode(",",$visible);
  78.     ereg_replace ( ",,", " ", $visible );
  79.      ereg_replace ( "_", " ", $visible );
  80.     ereg_replace ( "<a href=\"#\" ><img src=\"http://tmc-tectest/fileadmin/open_resources/image_library/icons/icon_delete.gif\" border=0 alt=\"$m\" onmousedown=\"changeText(this.alt)\" onmouseup=main.submit()></a>\n<BR>", " ", $visible );
  81.     echo("<INPUT type=hidden name=\"hidden\" value=$visible></input>");
  82.     echo("<input type=hidden id=\"desc\" name=\"remove\">");
  83.     echo("</FORM>");
  84.     echo("<FORM name=go action=\"$PHP_SELF?op=update&TID=$TID\" method=\"post\">");
  85.     echo("<INPUT type=hidden name=\"upload\" value=$visible></input>");
  86.     echo("<input type=submit value=Save onclick='this.form.submit();window.close()'>");
  87.     echo("<p class=tdborder style=\"width:300px\" align=center>to remove a erroneous selection from the list, click the trash icon.<br> to remove an entry from the database entirely, check the box and select \"save\"</p>");
  88.  
  89.     if($op=="update"){
  90.  
  91.         $upload=explode(",",$_POST[upload]);
  92.         $i=0;
  93.         $run=count($upload);
  94.         While($i<$run){
  95.         $IDSQL="SELECT DISTINCT `id` FROM serv_resp_service WHERE `service` LIKE \"$upload[$i]\"";
  96.         $IDQUERY=mysql_query($IDSQL) OR DIE (mysql_error().$IDSQL);
  97.         $ID=mysql_result($IDQUERY,0) OR DIE (mysql_error().$IDSQL);
  98.         $MID=$MID.",".$ID;
  99.         $i++;
  100.  
  101.     }    $MID=substr($MID, 1);
  102.         $MID=explode(",",$MID);
  103.         $MID=array_unique($MID); 
  104.  
  105.         $i=0;
  106.         $MID=array_unique($MID);
  107.         $run=Count($MID);
  108.  
  109.         while ($i<$run){
  110.  
  111.             $sql1="SELECT * FROM serv_resp_related WHERE (`source`=$TID AND `linked`=$MID[$i]) OR (`source`=$MID[$i] AND `linked`=$TID)";
  112.             $query1=mysql_query($sql1);
  113.             @$result1=mysql_num_rows($query1);
  114.             if ($result1=0){
  115.             $sql="INSERT INTO serv_resp_related (`source`,`linked`) VALUES ($TID,$MID[$i])";
  116.             print_r("$_POST[deleted]");
  117. //              $query=mysql_query($sql) OR DIE(mysql_error());
  118.              };
  119.             $i++;
  120.  
  121.         }
  122.  
  123.         $del=$_POST["deleted"];
  124.  
  125.         print_r($_POST);
  126.  
  127.         foreach ($del as $key => $value)
  128.           {
  129.         $sql="DELETE FROM serv_resp_related WHERE (`Source`=$value AND `Linked`=$TID) OR (`Source`=$TID AND `Linked`=$value";
  130.         echo("$sql<BR>");
  131.     }
  132.     }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.      ?>
Aug 24 '07 #1
5 3555
Atli
5,058 Expert 4TB
Hi.

Yes this is true. If you create <input> elements named 'name[]', PHP will threat all of them as an array.
Keep in mind tho, boxes that are not checked will not be included in the post.

For example.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Print the FORM
  3. $numberOfBoxes = 10;
  4. echo '<form action="?" method="POST">';
  5. for($i = 0; $i < $numberOfBoxes; $i++) {
  6.     echo '<br /><input type="checkbox" name="box[]" value="box'. $i .'" /> Box '. $i;
  7. }
  8. echo '<br /><br /><input type="submit" name="boxSubmit" />';
  9. echo '</form>';
  10.  
  11. // Check if the data has been posted an print it
  12. if(isset($_POST['boxSubmit'])) {
  13.     echo "<pre>";
  14.     echo "\n<b>Boxes checked:</b>";
  15.     foreach($_POST['box'] as $boxName) {
  16.         echo "\n - $boxName";
  17.     }
  18.     echo "</pre>";
  19. }
  20. ?>
  21.  
Will print out all the boxes you checked. The rest will not be a part of the array.
Aug 24 '07 #2
Tarantulus
114 100+
Hi.

Yes this is true. If you create <input> elements named 'name[]', PHP will threat all of them as an array.
Keep in mind tho, boxes that are not checked will not be included in the post.

For example.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Print the FORM
  3. $numberOfBoxes = 10;
  4. echo '<form action="?" method="POST">';
  5. for($i = 0; $i < $numberOfBoxes; $i++) {
  6.     echo '<br /><input type="checkbox" name="box[]" value="box'. $i .'" /> Box '. $i;
  7. }
  8. echo '<br /><br /><input type="submit" name="boxSubmit" />';
  9. echo '</form>';
  10.  
  11. // Check if the data has been posted an print it
  12. if(isset($_POST['boxSubmit'])) {
  13.     echo "<pre>";
  14.     echo "\n<b>Boxes checked:</b>";
  15.     foreach($_POST['box'] as $boxName) {
  16.         echo "\n - $boxName";
  17.     }
  18.     echo "</pre>";
  19. }
  20. ?>
  21.  
Will print out all the boxes you checked. The rest will not be a part of the array.
Thanks for confirming that, in that case i'm even more confused. if that is the case the code I am using should definately work, but for some reason the post for the checkboxes is never set...
Aug 24 '07 #3
Atli
5,058 Expert 4TB
I see you have two <form> elements. Are you sure you are submitting the right one?
Aug 24 '07 #4
pbmods
5,821 Expert 4TB
Heya, Tarantulus.

Have you tried submitting the form with the checkboxes checked? Checkboxes do not show up in form results at all unless they are checked.
Aug 24 '07 #5
Tarantulus
114 100+
Doh!

thanks Atli,

I noticed that the checkboxes were in the wrong form element.
Aug 28 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Froefel | last post: by
1 post views Thread by adrive | last post: by
53 posts views Thread by souporpower | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.