Jump to content

checkboxes, submit, and $_POST


mrherman

Recommended Posts

I have a group of rows from a db query.  The rows are displayed in a simple HTML table.  Each row in the table begins with a checkbox.  There is a submit button below the table.  The form method is POST.

 

This is my <input> statement:

 

<input type = 'checkbox' name = 'checkbox[]'
         id = '<?php echo $aCols[sid] ; ?>'
                        value = ' ' " 

 

After the user has selected some rows and clicked the submit button, shouldn't the $_POST array be updated automatically?  My $_POST array is showing as empty.  I have tried many different things, but I've had no success.

 

Thanks for your time and help.

Link to comment
Share on other sites

Thanks for getting back.  Yes, I've got it in my script like this:

 

<form name = 'form1'
    method = 'post'
    action = '<?php echo $PHP_SELF ; ?>'> 

 

Do I perhaps have it in the wrong place in the script?  I have it just before the <table> tags begin.

Link to comment
Share on other sites

Thought I'd go ahead and post my code, if you don't mind taking a gander.  I'm a beginner (although I've been a beginner for a long time).  So, warning -- you may need to cover your eyes.

 

<?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 ) == 0 ) 
   {
      exit ( "no rows returned" ) ;
   }
else
   {
      $fields_num = mysql_num_fields ( $qResult ) ;
      $nRowCount = mysql_num_rows ( $qResult ) ;
   }

while ( $aCols = mysql_fetch_array ( $qResult ) )  
   {   
      $output = "<table width       = \"100%\"
                        valign      = \"center\"
                        border      = \"1\"
                        cellpadding = \"1\"
                        cellspacing = \"1\" >" ;
                        
      $output .= "<tr>" ;
      
      for ( $i = 0 ; $i < $fields_num ; $i++ )
         {
            $oField = mysql_fetch_field ( $qResult, $i ) ;
            $output .= "<th> {$oField->name} </th>" ;
         }
      $output .= "</tr>\n"; 
      
      $output .= "<tr><td>" ;
      $output .= "<input type = 'checkbox'
                         name = 'checkbox[]'
                           id = '<?php echo $aCols[sid] ; ?>'
                        value = '' " ;  
      $output .= "</td>" ;
      
      for ( $n = 1 ; $n <= ( $fields_num - 1 ) ; $n++  )
         {
            $output .= "<td>" ;  
            $output .= $aCols [ $n ] ; 
            $output .= "</td>" ;   
         } 
      $output .= $aCols [ $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>

Link to comment
Share on other sites

I don't see anyplace in your script where you actually check the $_POST.  If this is planned to be a self posting script, then you need to have an if - then - else where you actually check for values in the $_POST and do something.  As it is now, this script will just repeatedly list out the database and inside the form.

Link to comment
Share on other sites

So if you look at the source you'll see clearly that there are two problems with your checkbox.  The first is that you are not emitting the name of "checkbox[]" so that will be a problem.  For checkboxes, they only exist in the post if the checkbox actually has the attribute of checked. 

 

Either the names need to be different for each checkbox or you need an array.  Additionally, your id value is broken, and is not parsing as php.  Additionally, the input tag for the checkboxes isn't closed properly, which could be related to the other issues you have with the code.  As your html is malformed it's not surprising that things aren't working.

Link to comment
Share on other sites

Just to be clear -- I didn't explain this well, but in a post if you use:

 

 

name = "checkbox[]" then you get an array.

 

If you use name = "checkbox" then you'll have a problem if there are 2 checkboxes with the same name, as you have currently.  I was surprised about this because your php source showed that you were using checkbox[] but perhaps something changed?

 

In either case, checkboxes are somewhat confusing in that their behavior is that if the person checks a checkbox it will show up in the $_POST, but if someone does not check it, then it will be completely missing from the $_POST.

Link to comment
Share on other sites

A trick I use to debug this type of thing is to use GET in my forms while developing so that I can easily check what's being posted by looking at the URI in the address bar after form submission. Once I've got the parameters and values that I want being posted I'll switch it back to GET. It's an easy way to figure out if there are any problems upstream of any PHP I've written to handle the form submission.

 

-Dave

Link to comment
Share on other sites

Hi, gizmola

 

Yes, you are right.  I checked and I had "configured" with the "[]" brackets and changed them in the interim.  I forgot to change them back before running the html source code from the original posting.  Actually, I had never thought of doing looking at the source code, and none of my beginners' books have mentioned it.

 

But, yes, I see what you mean.  Thanks again for the pointers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.