tommy168 Posted March 29, 2011 Share Posted March 29, 2011 Hey guys I am confused about using checkboxes to delete entries. Yes, just for delete. Codes for table: echo "<form method = \"post\" action=\"{$_SERVER['PHP_SELF']}\"> <table> <tr> <td width=\"55\" class=\"formLabelsS2\"><input type=\"submit\" name=\"delete\" value=\"Delete\" id=\"delete\"></td> <td> Please note that entries could not be restored once they are deleted!</td> </tr> </table> </form>"; echo "<table class=\"sortable\" id=\"query_quick1\" width=\"100%\" >\r\n"; echo "<thead>"; echo "\t<tr><th></th><th></th><th>Up.dated Query</th><th>Link</th><th>Promoter</th><th> # </th><th>Up?</th> <th>Ana. Area</th><th> # </th><th>Up?</th> <th>C. Type</th><th> # </th><th>Up?</th><th>Other C. Type</th> <th> # </th><th>Up?</th><th>Gen. Back.</th><th> # </th><th>Up?</th> <th>Other Gen. Back.</th><th> # </th><th>Up?</th><th>Author</th><th> # </th><th>Up?</th> <th>Other</th><th> # </th><th>Up?</th><th>Date</th><th>Email</th> <th>F. Name</th><th>L. Name</th></tr>\r\n"; echo "</thead>"; if($result->num_rows){ while ($row = $result->fetch_array()){ $RowCount ++; $row_color = ($RowCount % 2) ? $color1 : $color2; echo "\t<tr class=\"$row_color\" > <form method = \"post\" action=\"{$_SERVER['PHP_SELF']}\"> <td><input type =\"hidden\" name = \"id\" value=\"{$row['id']}\"></td> <td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"{$rows['id']} \"></td> <td>{$row['query']}</td><td>{$row['link']}</td> <td>{$row['pro']}</td><td>{$row['pro_count']}</td> <td>{$row['pro_update']}</td> <td>{$row['ana']}</td><td>{$row['ana_count']}</td><td>{$row['ana_update']}</td> <td>{$row['cell']}</td><td>{$row['cell_count']}</td><td>{$row['cell_update']}</td> <td>{$row['cellother']}</td><td>{$row['cellother_count']}</td><td>{$row['cellother_update']}</td> <td>{$row['gen']}</td><td>{$row['gen_count']}</td><td>{$row['gen_update']}</td> <td>{$row['genother']}</td><td>{$row['genother_count']}</td><td>{$row['genother_update']}</td> <td>{$row['author']}</td><td>{$row['author_count']}</td><td>{$row['author_update']}</td> <td>{$row['other']}</td><td>{$row['other_count']}</td><td>{$row['other_update']}</td> <td>{$row['date']}</td><td>{$row['Email']}</td> <td>{$row['First_Name']}</td><td>{$row['Last_Name']}</td> </form></tr>"; } } echo "</table>"; php for deleting entries: } elseif(isset($_SESSION['user_id']) AND isset($_POST['delete'])){ $connect=db_connect_2(); foreach($_POST['checkbox'] as $check) { $delete = mysqli_query("DELETE FROM mailing_list WHERE id = '$check'"); } $body = "mailingList.php"; } Error pops up: Warning: Invalid argument supplied for foreach() Of course no entries are deleted due to this warning. Help would be greatly appreciated as always Thanks. Link to comment https://forums.phpfreaks.com/topic/232054-checkboxes/ Share on other sites More sharing options...
linus72982 Posted March 29, 2011 Share Posted March 29, 2011 I may not be up on the latest things that HTML can do, but it looks like you are mixing php and html without ending and closing tags (<?php and ?>). Granted, you can use things like while loops and if statements with HTML, but you have to do it like so: <?php if (condition): ?> <html tags> content <more tags> <?php endif; ?> Also, you use rows['id'] at one point and row['id'] at another - one with an s and one without. The main error you are getting, however, is that foreach is supplied a non array to iterate, I believe. Looking through the code, I only see one input attached to the name "checkbox" and then in the PHP you are trying to iterate through an array of that name. Oh, this is the line that uses the checkbox name, and the only one that I saw: <td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"{$rows['id']} \"></td> Link to comment https://forums.phpfreaks.com/topic/232054-checkboxes/#findComment-1193671 Share on other sites More sharing options...
Pikachu2000 Posted March 29, 2011 Share Posted March 29, 2011 You can't split up a form like that. Link to comment https://forums.phpfreaks.com/topic/232054-checkboxes/#findComment-1193672 Share on other sites More sharing options...
Pikachu2000 Posted March 29, 2011 Share Posted March 29, 2011 I may not be up on the latest things that HTML can do, but it looks like you are mixing php and html without ending and closing tags (<?php and ?>). Granted, you can use things like while loops and if statements with HTML, but you have to do it like so: <?php if (condition): ?> <html tags> content <more tags> <?php endif; ?> Also, you use rows['id'] at one point and row['id'] at another - one with an s and one without. The main error you are getting, however, is that foreach is supplied a non array to iterate, I believe. Looking through the code, I only see one input attached to the name "checkbox" and then in the PHP you are trying to iterate through an array of that name. Oh, this is the line that uses the checkbox name, and the only one that I saw: <td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"{$rows['id']} \"></td> You can echo html from php without any problems. The name attribute, specified as checkbox[], makes it an array. Link to comment https://forums.phpfreaks.com/topic/232054-checkboxes/#findComment-1193675 Share on other sites More sharing options...
linus72982 Posted April 1, 2011 Share Posted April 1, 2011 Oh, I see what he was doing. I'm used to seeing echo'd lines looking like this: echo "blah blah" ."more html"; It's confusing to see the tags in there without quotes to start out like the convention above. I see the where the quotes begin and end now, though. Link to comment https://forums.phpfreaks.com/topic/232054-checkboxes/#findComment-1195634 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.