TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-01-2010, 05:40 AM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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.
Dave is offline  
Reply With Quote
Old 02-01-2010, 07:30 AM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

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...
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
The Following User Says Thank You to maeltar For This Useful Post:
Dave (02-01-2010)
Old 02-01-2010, 02:02 PM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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
Dave is offline  
Reply With Quote
Old 02-01-2010, 03:17 PM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

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";

?>
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
The Following User Says Thank You to maeltar For This Useful Post:
Dave (02-01-2010)
Old 02-02-2010, 03:17 AM   #5 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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>
Dave is offline  
Reply With Quote
Old 02-02-2010, 07:20 AM   #6 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

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

print_r($GLOBALS)
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
The Following User Says Thank You to maeltar For This Useful Post:
Dave (02-02-2010)
Old 02-02-2010, 12:50 PM   #7 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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!
Dave is offline  
Reply With Quote
Old 02-28-2010, 08:09 PM   #8 (permalink)
The Wanderer
 
Join Date: Aug 2009
Posts: 17
Thanks: 0
Rhinos is on a distinguished road
Default

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.
Rhinos is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fancy Form checkboxes Kay1021 General 13 06-02-2009 08:53 PM
new JS project... allworknoplay Javascript, AJAX, E4X 21 04-25-2009 11:27 PM
How to locate certain checkboxes benton Javascript, AJAX, E4X 4 01-08-2009 11:04 PM
jQuery and firefox checkboxes?? drewbee Javascript, AJAX, E4X 1 08-27-2008 05:54 PM


All times are GMT. The time now is 11:22 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design