473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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

Similar topics

3
by: sammmista | last post by:
hello experts, Plz dont treat this as another newbie query , i did my homework but still getting nowhere :( :( :( Trying to learn PHP on Fedora core 1 (PHP 4.3,MySQL,HTTPD).Unable to post data...
0
by: ginette | last post by:
I really need help with a strange problem. i have a page having ONLY this cleaned and washed HTML code, no client scripting at all and "some" folks are unable to post any file till "some" are...
10
by: Froefel | last post by:
This is the second time in a few weeks that I write posts that never show up in the group. Is there something wrong with this group? Does anyone have similar experiences? What are you all using...
1
by: adrive | last post by:
guys, I can't seem to find any info on how to pass a list of checked checkboxes through POST method of ajax. Unlike traditional forms, when you submit a button, a list of selected checkboxes...
2
by: runway27 | last post by:
i am using a self submitting form <form action="<?php echo $_SERVER; ?>" method="POST" id="test2" name="test1"> i need to do a validation of textfields, checkboxes, radio buttons i am able...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
5
by: mamul | last post by:
Hi All, Could you please someone help me. Thanks in advance. Actually my application is building fine. when i tries to run this from visual studion run option, it showing me error . But this is...
1
by: sandeepsangshetty | last post by:
Hi Friends, I'm unable find check box control in my webpage. When building the page I'm getting "checkbox null" and "false". Can any help me to find the solution. I'm pasting the code here. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.