473,782 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to pull $_POST for checkbox?

Tarantulus
114 New Member
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 3875
Atli
5,058 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
I see you have two <form> elements. Are you sure you are submitting the right one?
Aug 24 '07 #4
pbmods
5,821 Recognized Expert Expert
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 New Member
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
3542
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 from html form to php file(connecting to mysql database and inserting into a table) . This seemingly simple problem is making me go mad !!! I googled extensively and already tried the following
0
1328
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 able. All using IE6 (SP1 or not) Leaving the <input type="file"> unassigned is ok for everyone but when you select a file, even a SMALL SMALL SMALL 10 bytes almost empty txt file, the form fails to be submitted.
10
1293
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 to read and write posts in news groups? I use Google Groups but I'm getting suspicious of its quality... -- Hans
1
2261
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 with the same name will allow you to retrieve it at the server side (php in my case). IS this the only way? param = "selecteditem = " + document.myform.item.value; param+ = "&selecteditem = " + document.myform.item.value;
2
2813
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 to read, display and validate textfields after the form has been submitted however i am getting an error for checkbox and radio buttons.
53
8418
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 language="javascript" type="text/javascript">
5
1751
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 working from the exe file in the release folder. when i am running the exe file of the application it is working fine. I want to run from VIsual studio. Plz help me someone. There are two database are used in my application. With one database it...
1
5560
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. ------------------------------------------------------------------------------- _BigCart.ascx
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7492
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.