TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Checkboxes and db rows (http://www.talkphp.com/absolute-beginners/5245-checkboxes-db-rows.html)

Dave 02-01-2010 05:40 AM

Checkboxes and db rows
 
Hello again.

I have a query that pulls rows from a mysql db. The data is displayed in a simple HTML table. The first column of the HTML table has a checkbox for each row. It looks a little like this:


PHP Code:

+-----------+----------+----------+
|   
SELECT  |    NAME  |    ID    |
+-----------+----------+----------+
|   [
ckbx]  |    John  |    2233  |
+-----------+----------+----------+
|   [
ckbx]  |    Susie |    5577  |
+-----------+----------+----------+

SUBMIT 


The user is allowed to select rows, each of which has a unique ID. I have spent untold hours trying to understand how to know which rows (i.e., the unique ids) were selected by checkboxes. How would I go about this?

The HTML table is on a form with a POST method. So, for example, if a user checks the checkbox on John's row, how can I retrieve John's ID into a $_POST array?

None of my books go into this specifically. They only give checkbox examples such as What is your fav. ice cream? Vanilla, Chocolate, blah, blah.

Thanks!
Dave

Note: the above table is, of course, simplified. The HTML table actually contains about a dozen fields and up to a couple of hundred records resulting fromt the db query.

maeltar 02-01-2010 07:30 AM

You are looking at something like....

> # jump out of php code

<INPUT TYPE=CHECKBOX NAME=" <? $id > ">

<?
rest of php code

can't off the top of my head remember if the quotes are required or not, but basically that will create a checkbox and name it the id of the current row pulled from the db...

So you need to either jump out of your php code, similar to above, or just echo the html code... Not sure which is best, or most efficient, but either method will work...

Dave 02-01-2010 02:02 PM

Thanks, maeltar...

Yes, I have all that set up and I have tried many unsuccessful variations. I really need to know what goes into the attributes of the <INPUT...>, and also how to process this at the end of the script.

PHP Code:

<snip...>
$output .= "<input type = 'checkbox'
                   name = (WHAT GOES HERE?)
                     id = (WHAT GOES HERE?)
                  value = (WHAT GOES HERE?)" 


...and also the process for getting this into the $_POST array.

Dave

maeltar 02-01-2010 03:17 PM

To be honest I would not code it the way you have done, I would be (maybe wrongly) do it something like this...

Code:


<?

print "<table cellpadding=2 cellspacing=0 border=0 >\n";
print "<tr>
<th>Select</th>
<th>Name</th>
<th>ID</th>
</tr>\n";

while ($qry = mysql_fetch_array($my_query))
{
print "<tr><td>INPUT TYPE=CHECKBOX NAME=$qry[ID]</td>
<td>$qry[Name]</td>
<td>$qry[ID]</td>
<form name='form1' method='post' action=$PHP_SELF>
<input type='submit'>
</form>
</td>
</tr>\n ";
}
print "</table>\n";

?>


Dave 02-02-2010 03:17 AM

Thanks again. I've reworked my script and have posted it below. The problem is that after some rows are selected by checkboxes and the submit button is clicked, the $_POST array is empty, using print_r($_POST) to check.

Any suggestions? -- Dave

PHP Code:

<?php 
session_start 
() ;
?>

<h1>Data Browse - Edit - Export</h1>

<?php 

$host     
'localhost' ;
$root     'root' ;
$db_pass  'password' ;
$db       'proj_sap' ;
$tb_data  't_tims0809' ;
$schcode  "320" $_SESSION 'school_code' ] ;

$sql "
        SELECT ''            AS selection
               , last_name   AS lastname
               , first_name  AS firstname
               , grade       AS gr
               , ethnic      AS eth
               , sex         AS sex
               , student_id  AS sid
               , reason      AS reas
               , mon_init    AS month
          FROM 
{$tb_data}
         WHERE tag <> '' AND 
               tag IS NOT NULL AND
               schcode = 
{$schcode}
         LIMIT 1 
       " 
;
?>

<!-- FORM -->
<form name = 'form1'
    method = 'post'
    action = '<?php echo $_SERVER["PHP_SELF"] ; ?>'> 
          
<?php 
echo "<h2>School: " $schcode "</h2>" ;
echo 
"<h2>Category: " "current students" "</h2>" ;
echo 
"<hr>" ;

$db      "proj_sap";
$db_user "root";
$pass    "teacher";
$host    "localhost";

mysql_connect $host$db_user$pass 
   || exit ( 
"problem: " mysql_error () ) ;
   
mysql_select_db $db 
   || exit ( 
"cannot connect to DB: " mysql_error() ) ;

$qResult mysql_query $sql ) ;

mysql_close();

if ( 
mysql_num_rows $qResult ) == 
   {
      exit ( 
"no rows returned" ) ;
   }
else
   {
      
$fields_num mysql_num_fields $qResult ) ;
      
$nRowCount mysql_num_rows $qResult ) ;
   }
      
$output "<table width       = \"100%\"
                        valign      = \"center\"
                        border      = \"1\"
                        cellpadding = \"1\"
                        cellspacing = \"1\" >" 
;
                        
      
$output .= "<tr>" ;
      
      for ( 
$i $i $fields_num $i++ )
         {
            
$oField mysql_fetch_field $qResult$i ) ;
            
$output .= "<th> {$oField->name} </th>" ;
         }
      
$output .= "</tr>\n"
      
while ( 
$aRecords mysql_fetch_array $qResult ) )  
   {      
      
$output .= "<tr><td>" ;
      
$output .= "<input type = 'checkbox'
                         name = 'checkbox[]'
                           id = '<?php echo 
$aRecords[sid] ; ?>'
                        value = '' " 
;  
      
$output .= "</td>" ;
      
      for ( 
$n $n <= ( $fields_num ) ; $n++  )
         {
            
$output .= "<td>" ;  
            
$output .= $aRecords $n ] ; 
            
$output .= "</td>" ;   
         } 
      
$output .= $aRecords $n ] ; 
      
$output .= "</td>\n"
      
$output .= "</tr>\n" 
   
   } 
// End while 
$output .= "</table>\n"
echo 
$output ;
?>

<!-- submit button here -->
<br /> <br />
<input type = "submit"
      value = "Select Students" />
</form>
</body>
</html>


maeltar 02-02-2010 07:20 AM

Have a look and see if you are getting any data at all...

print_r($GLOBALS)

Dave 02-02-2010 12:50 PM

Will do. Also, I'm going to go through this thing again with a fine-tooth comb and will hopefully find something. It is interesting how this little thing has given me so much trouble!

Rhinos 02-28-2010 08:09 PM

For the line where you output the checkbox you need to make the following changes

PHP Code:

      $output .= "<input type = 'checkbox'
                         name = 'checkbox[]'
                           id = 'checkbox_
$aRecords[sid]'
                        value = '
$aRecords[sid]' />"

the id attribute is mainly used in DOM manipulation with javascript. You need to set the value attribute to the ID of the record.
You also did not properly close the <input> tag as you missed out the closing />

When the form is submitted now you should be able to do a var_dump($_POST['checkbox']). If it is empty it's because you didn't check any of the boxes. So try checking a few boxes and then it should give you an array with the IDs of those records that you selected.


All times are GMT. The time now is 04:46 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0