Jump to content

HTML table with checkboxes -- totally baffled!!


mrherman

Recommended Posts

I have a table with thousands of records.  On each displayed page, only 20 rows are showing, with the following fields:

 

check	 last_name 	 first_name 	 gr 	 eth 	 sex 	 id_num 	 reason 	 start_date

 

The "check" field indicates a checkbox that the user can tick.  At the bottom of each page is the select button.  I want to take the rows that are selected by the user and insert the unique "id_num" into a different table. 

 

This is the code I have used without success:

 

CHECKBOX:

 

$html = <input type='checkbox' name='checkbox[]'
                      id = '' value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;

 

 

FOR THE SQL INSERT AFTER THE ROWS ON A PAGE ARE SELECTED:

 

include_once "db_login.php" ;
if ( isset ( $_POST [ checkbox] ) ) 
   { 
      foreach ( $_POST [ 'checkbox' ] as $checkbox )
         {
            $sql = "INSERT INTO sap_id_select (  select_date
                                               , select_schcode
                                               , select_user
                                               , select_id )
                    VALUES (  CURRENT_DATE ()   // this is INSERTed
                            , '{$_SESSION [ school_code ]}'   // this is INSERTed
                            , '{$_SESSION [ user_name ]}'   // this is INSERTed
                            , '{$checkbox}'  // ** This does not work!!
                           ) " ;
            mysql_query ( $sql ) or ( "Error " . mysql_error () ) ;
         }
   }

 

I hope this is enough code for some help.  I have spent many hours on this trying different things, and with some help already from PHPFREAKS, but I'm at this stage and am absolutely stuck.  Thanks!

Thanks again, Teamatomic. 

 

It already is in my form.  I didn't say it very clearly, but the "input" code I posted is, in fact, within the <form> </form> tags.

 

The problem, so far as I can tell, is that when a user checks a box, the $_POST does not pick up anything.  If I select, say, 2 rows, the $_POST array has 3 elements, but they are empty:

 

Array
(
    [0] => 
    [1] => 
)

 

Thanks again!

Hey, Teamatomic -- Here is the form.  Sorry about the "mess," but I'm a learner...lot of crap in here...

 

 

<!-- Start HTML form -->
<form name = 'form1'
    method = 'post'
    action = ''> 

<?php
$qResult = mysql_query ( $sql ) ;
if ( mysql_num_rows ( $qResult ) == 0 ) 
   {
      exit ( trigger_error ( "SQL", E_USER_ERROR ) ) ;
   }
else
   {
      $fields_num   = mysql_num_fields ( $qResult ) ;
      $nRowCount    = mysql_num_rows ( $qResult ) ;
      $nRowsPerPage = 20 ;
   }

// Start pagination script; Initialize scoped variables.

// Number of rows to be displayed.
//$nCurrentPage       ; // Current page.
//$nTotalPages    = 0 ; // Total pages containing records.
//$nOffset        = 0 ; // Start record for each page.
//$nAdjacentPages = 0 ; // Page +/- links relative to current page.
//$nNextPage      = 0 ; // Page after current page.
//$nPreviousPage  = 0 ; // Page before current page.

// Calculate the total number of pages to be displayed.

$nTotalPages = ceil ( $nRowCount / $nRowsPerPage ) ;

if ( isset ( $_GET [ 'nCurrentPage' ] ) and 
     is_numeric ( $_GET [ 'nCurrentPage' ] ) )
   {
      $nCurrentPage = ( int ) $_GET [ 'nCurrentPage' ] ;
   }
else
   {
      $nCurrentPage = 1 ;
   }
   
if ( $nCurrentPage > $nTotalPages )
   {
      $nCurrentPage = $nTotalPages ;
   }
   
if ( $nCurrentPage < 1 ) 
   {
      $nCurrentPage = 1  ;
   }

// Incorporate the sort order that the user selected.

$cSortMethod = $_SESSION [ sortmethod ] ;
switch ( $cSortMethod )
   {
      case "grade" :
         $cSortMethod = "gr, last_name, first_name" ;
         break ;
      case "lastname" :
         $cSortMethod = "last_name, first_name" ;
         break ;
      case "id" :
         $cSortMethod = "student_id" ;
         break ;
      otherwise ;
   }

// 'Offset' is first record to display on current page.

$nOffset = ( $nCurrentPage - 1 ) * $nRowsPerPage ;

// Concatenate ORDER BY and LIMIT statement to $sql.

$sql .= "\n" . "ORDER BY {$cSortMethod}" ;
$sql .= "\n" . "LIMIT {$nOffset}, {$nRowsPerPage}" ;

$qResult = mysql_query ( $sql ) or ( "//Error// " . mysql_error() ) ;
if ( mysql_num_rows ( $qResult ) == 0 ) 
   {
      exit ( trigger_error ( "SQL_ERROR!!!", E_USER_ERROR ) ) ;
   }

//===============================================================
//                                                    HTML table.
//===============================================================

$html = "<table width       = \"730\"
                align       = \"center\"
                border      = \"1\"
                cellpadding = \"1\"
                cellspacing = \"1\" >" ;
                  
// Column headings.  Note: "mysql_fetch_field" yields an object.

$html .= "<tr>" ;
$html .= "<th>select</th>" ;
for ( $i = 0 ; $i < $fields_num ; $i++ )
   {
      $oField = mysql_fetch_field ( $qResult, $i ) ;
      $html .= "<th> {$oField->name} </th>" ;
   }
$html .= "</tr>\n"; 

// Loop through records and build the HTML table.

while ( $aRecords = mysql_fetch_array ( $qResult ) )
   {    
      //$cInputValue = $aRecords ["sid"] ; 
      $html .= "<tr><td width = '8%'>" ;
      
      // Checkbox in the first cell of row.
      //
      $html .= "<input type='checkbox'
                      name='checkbox[]'
                      id = '' 
                      value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;

//      $html .= "<input type = 'checkbox'
//                         name = 'checkbox[]'
//                           id = ''
//                        value = '$cInputValue' >" ;  

      $html .= "</td>" ;
      for ( $nCol = 0 ; $nCol <= ( $fields_num - 1 ) ; $nCol++  )
         {
            $html .= "<td width = '%'>" ;  
            $html .= $aRecords [ $nCol ] ; 
            $html .= "</td>" ;   
         } 
      $html .= $aRecords [ $nCol ] ; 
      $html .= "</td>\n" ; 
      $html .= "</tr>\n" ; 
   } // End while 
   
$html .= "</table>\n"; 
$html .= "<br>" ;
?>

<div id="browsedata">

<?PHP
//===============================================================
//                                                    Pagination.
//===============================================================
$nAdjacentPages =  3; 

// If the current page number is greater than 1, then display:
// "<<" and "<" (i.e., << < ).

if ($nCurrentPage > 1) 
   {
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=1'> << </a> " ;
      $nPreviousPage = $nCurrentPage - 1 ;
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=$nPreviousPage'> < </a> ";
   }   

// Example: appearance of page links when viewing page 5:
//     << <  2  3  4  [5]  6  7  8  > >>  

for ( $x = ( $nCurrentPage - $nAdjacentPages ) ; 
      $x < ( ( $nCurrentPage + $nAdjacentPages ) + 1 ) ; 
      $x++ ) 
   {
      // if it's a valid page number...
      
      if ( ( $x > 0 ) and ( $x <= $nTotalPages ) ) 
         {
            // If on current page, 'highlight' but do not link.
            // If not current page, make it a link.

            if ( $x == $nCurrentPage ) 
               {
                  echo " [<b> $x </b>] " ;
               } 
            else 
               {
                  echo " <a href=
                     '{$_SERVER['PHP_SELF']}?nCurrentPage=$x'> $x </a> " ;
               } 
         } 
   } 

// If not last page, show '>' and '>>' links.

if ( $nCurrentPage != $nTotalPages ) 
   {
      $nNextPage = $nCurrentPage + 1;
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=$nNextPage'> > </a> ";
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=$nTotalPages'> >> </a> ";
   } 
?>

</div> <!-- browsetitle -->

<?PHP
//===============================================================
//                                       BOTTOM OF TABLE DISPLAY.
//===============================================================
echo $html;  
echo "Some instructions here...<br/>" ;
?>

<!-- submit button here --> 
<br />
<div id="browsetitle">
<input type = "submit"
       value = "Select Students" />
</form>
</div>

This is broken:

$html .= "<input type='checkbox'

                      name='checkbox[]'

                      id = ''

                      value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;

 

You are already in php. Why use php tags again? Do this!

$html .= "<input type='checkbox' name='checkbox[]'

id = '' value='$aRecords ['id_num']' />" ;

 

 

HTH

Teamatomic

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.