mrherman Posted February 1, 2010 Share Posted February 1, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/ Share on other sites More sharing options...
gizmola Posted February 1, 2010 Share Posted February 1, 2010 Are you actually using the post method in your form? Link to comment https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005168 Share on other sites More sharing options...
mrherman Posted February 1, 2010 Author Share Posted February 1, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005170 Share on other sites More sharing options...
gizmola Posted February 1, 2010 Share Posted February 1, 2010 The form simply needs to be around the elements you're submitting. Also, why are you using $PHP_SELF? That should be $_SERVER['PHP_SELF']. How are you accessing the post variables -- hopefully using $_POST[]? Link to comment https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005173 Share on other sites More sharing options...
mrherman Posted February 1, 2010 Author Share Posted February 1, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005200 Share on other sites More sharing options...
gizmola Posted February 1, 2010 Share Posted February 1, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005213 Share on other sites More sharing options...
mrherman Posted February 2, 2010 Author Share Posted February 2, 2010 I did a "print_r ( $_POST )" after running the script and clicking submit, but the $_Post array has no information. Some kind of generic code to populate the $_POST would be helpful and would be very much appreciated. Thanks again. Link to comment https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005260 Share on other sites More sharing options...
gizmola Posted February 2, 2010 Share Posted February 2, 2010 Please run the script and take the html source and copy it here so we can take a look. Link to comment https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005291 Share on other sites More sharing options...
mrherman Posted February 2, 2010 Author Share Posted February 2, 2010 Here is the source code. Thanks for taking a look! [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005295 Share on other sites More sharing options...
gizmola Posted February 2, 2010 Share Posted February 2, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005300 Share on other sites More sharing options...
mrherman Posted February 2, 2010 Author Share Posted February 2, 2010 OK, thanks. I'll work on those issues. I appreciate the time you spent on this! Link to comment https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005304 Share on other sites More sharing options...
gizmola Posted February 2, 2010 Share Posted February 2, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005312 Share on other sites More sharing options...
Haberdasher Posted February 2, 2010 Share Posted February 2, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005319 Share on other sites More sharing options...
mrherman Posted February 2, 2010 Author Share Posted February 2, 2010 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 https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005454 Share on other sites More sharing options...
mrherman Posted February 2, 2010 Author Share Posted February 2, 2010 Haberdasher Ah, that is a good thought. I'm going to try that. My beginner's books go into lengths to explain the diff. between GET and POST, but I have not seen your tip discussed as a way to identify bugs or bad programming. Thanks! Link to comment https://forums.phpfreaks.com/topic/190583-checkboxes-submit-and-_post/#findComment-1005462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.