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
- <?PHP
- //$visible=array();
- echo("<LINK REL=StyleSheet HREF=\"stylesheet.css\" TYPE=\"text/css\">");
- function push(&$array, &$object){
- $array[] =& $object;
- }
- function CleanA($array) {
- while ($count < count($arr)) {
- if ($arr[$count] == "") {
- unset($arr[$count]);
- }
- $count++;
- }
- return $arr;
- }
- echo("<BODY scroll=\"auto\"><script type=\"text/javascript\">\nfunction changeText(val){\nvar txtArea = document.getElementById('desc');\ntxtArea.value = val;\n}\n</script>");
- $datetoday=date("d/m/y");
- include("dbconnect.php");
- db_connect();
- echo("<BR><center><FORM name=main action=$PHP_SELF?TID=$TID method=\"post\" >");
- echo("<SELECT name=\"related\" onChange=this.form.submit()><OPTION></OPTION>\n");
- $stkgrpqueryedit="SELECT DISTINCT id, service FROM serv_resp_service ORDER BY service ASC";
- $stkgrpsqledit=mysql_query($stkgrpqueryedit)or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $stkgrpqueryedit");
- $i=0;
- $lenedit=mysql_numrows($stkgrpsqledit);
- while ($i<$lenedit){
- $stkgrpname=mysql_result($stkgrpsqledit,$i,"service")or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $stkgrpqueryedit");
- $stkgrpid=mysql_result($stkgrpsqledit,$i,"id")or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $stkgrpqueryedit");
- echo("<OPTION>$stkgrpname</OPTION>\n");
- $i++;
- };
- echo("</SELECT>");
- $hidden="$existing,$_POST[hidden]";
- $hidden=ereg_replace(" ","_",$hidden);
- $visible=explode(",",$hidden);
- $_POST[related]=ereg_replace(" ","_",$_POST[related]);
- push($visible,$_POST[related]);
- if(isset($_POST[remove])){
- unset($visible[$_POST[remove]]);
- }
- $run=count($visible);
- $m=0;
- echo("<BR><TABLE>");
- while ($m<$run){
- // $visquery="SELECT name from stk_groups WHERE id=$visible[$m]";
- // $visrun=mysql_query($visquery);
- // $visresult=mysql_result($visrun,0)or die("Error ".mysql_errno().": ".mysql_error()."\nQuery: $visquery");
- if($visible[$m]!=""){
- 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>");
- }$m++;
- }
- if($m=$run){
- echo("</TABLE>");
- }
- foreach($visible as $key => $value) {
- if($value == "") {
- unset($visible[$key]);
- }
- }
- $visible = array_values($visible);
- if($op=="upload"){
- // insertSQLhere
- }
- $visible=implode(",",$visible);
- ereg_replace ( ",,", " ", $visible );
- ereg_replace ( "_", " ", $visible );
- 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 );
- echo("<INPUT type=hidden name=\"hidden\" value=$visible></input>");
- echo("<input type=hidden id=\"desc\" name=\"remove\">");
- echo("</FORM>");
- echo("<FORM name=go action=\"$PHP_SELF?op=update&TID=$TID\" method=\"post\">");
- echo("<INPUT type=hidden name=\"upload\" value=$visible></input>");
- echo("<input type=submit value=Save onclick='this.form.submit();window.close()'>");
- 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>");
- if($op=="update"){
- $upload=explode(",",$_POST[upload]);
- $i=0;
- $run=count($upload);
- While($i<$run){
- $IDSQL="SELECT DISTINCT `id` FROM serv_resp_service WHERE `service` LIKE \"$upload[$i]\"";
- $IDQUERY=mysql_query($IDSQL) OR DIE (mysql_error().$IDSQL);
- $ID=mysql_result($IDQUERY,0) OR DIE (mysql_error().$IDSQL);
- $MID=$MID.",".$ID;
- $i++;
- } $MID=substr($MID, 1);
- $MID=explode(",",$MID);
- $MID=array_unique($MID);
- $i=0;
- $MID=array_unique($MID);
- $run=Count($MID);
- while ($i<$run){
- $sql1="SELECT * FROM serv_resp_related WHERE (`source`=$TID AND `linked`=$MID[$i]) OR (`source`=$MID[$i] AND `linked`=$TID)";
- $query1=mysql_query($sql1);
- @$result1=mysql_num_rows($query1);
- if ($result1=0){
- $sql="INSERT INTO serv_resp_related (`source`,`linked`) VALUES ($TID,$MID[$i])";
- print_r("$_POST[deleted]");
- // $query=mysql_query($sql) OR DIE(mysql_error());
- };
- $i++;
- }
- $del=$_POST["deleted"];
- print_r($_POST);
- foreach ($del as $key => $value)
- {
- $sql="DELETE FROM serv_resp_related WHERE (`Source`=$value AND `Linked`=$TID) OR (`Source`=$TID AND `Linked`=$value";
- echo("$sql<BR>");
- }
- }
- ?>