noobdood Posted May 16, 2014 Share Posted May 16, 2014 Hi guys! im new to phpfreaks *waving* im trying to delete row from a table that is populated by data from database using checkbox(and it doesnt matter if its just one checkbox or multiple) but its not working, as in nothing happens. no delete, no errors or warnings, just a refresh. table: <form class="event-button-wrap" method="post" action="event.php"> <div class="button-wrap"> <input type="submit" id="del_event" name="del_event" value="Delete Event"/> </div> <div class="event-list-table2" > <table class="event-table"> <thead> <tr> <th>Event Name</th> <th class="head">Event Date</th> <th class="head">Venue</th> </tr> </thead> <tbody class="tbody-event-list-table2"> <?php require "connection.php"; $check = mysql_query("SELECT * FROM event"); if(mysql_num_rows($check) > 0) { while($row = mysql_fetch_array($check)) { $id = $row['event_id']; $name = $row['event_name']; $dstart = $row['start_date']; $venue = $row['event_venue']; echo "<tr> <td> <input type='checkbox' name='check[]' class='check' value='$id'>$name </td> </tr>"; } } else { echo "<tr> <td colspan='4'>There Are No Events.</td> </tr>"; } ?> </tbody> </table> </div> </form> process is before <!DOCTYPE html>: <?php if(isset($_POST['del_event'])) { if(isset($_POST['check'])) { foreach($_POST['check'] as $del_id) { $del_id = (int)$del_id; require "connection.php"; $sqlqueer = mysql_query("DELETE FROM event WHERE event_id = $del_id"); if($sqlqueer) { echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">"; } } } ?> thanks in advance! Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted May 16, 2014 Share Posted May 16, 2014 <input onclick="window.href=... You must trigger a jquery (javascript) mechanism to submit the form/ or open a link in order to achieve it. https://www.google.pl/search?q=jquery+oncheck+checkbox&oq=jquery+oncheck&aqs=chrome.1.69i57j0l5.9299j0j1&sourceid=chrome&es_sm=93&ie=UTF-8 Quote Link to comment Share on other sites More sharing options...
noobdood Posted May 16, 2014 Author Share Posted May 16, 2014 (edited) but its in php + mysql. why isn't that enough? plus i really dont want to have to resort to using js when this should be enough. Edited May 16, 2014 by noobdood Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted May 16, 2014 Share Posted May 16, 2014 (edited) because you want to trigger an event php is static on the website: it's only subjected to PARSING the sent data You can do it with HTML, but would require to click SUBMIT some sort of (input). You want to do it, without -> jquery (javascript) The last, basically: listens & when the checkbox is ticked: it submits the form or directs to a link (even behind the layer of website) to do job wanted by you. Edited May 16, 2014 by AndyPSV Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted May 16, 2014 Share Posted May 16, 2014 I've read the part that you want to delete a row AFTER clicking checkbox, so you need JQUERY. https://www.google.pl/search?q=php+deleting+rows+checkbox&oq=php+deleting+rows+checkbox&aqs=chrome..69i57j0l2.4236j0j1&sourceid=chrome&es_sm=93&ie=UTF-8 It's simply to be done. http://blog.themeforest.net/tutorials/deleting-multiple-records-with-php/ 1. Firstly: use var_export($_POST); die; to see what it shows/ to get what you're receiving: then, delete it in DB It's simple. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 16, 2014 Share Posted May 16, 2014 Delete all the selected ids in a single query (EG ... WHERE id IN (1,2,3) ) instead of in loop. <?php require "connection.php"; // only connect once, not in a loop if(isset($_POST['del_event'])) { if(isset($_POST['check'])) { $del_ids = join(',', array_map('intval', $_POST['check'])); $sqlqueer = mysql_query("DELETE FROM event WHERE event_id IN ($del_ids)"); if($sqlqueer) { header("Location: event.php"); } else { die ("Error in delete query"); } } } ?> Quote Link to comment Share on other sites More sharing options...
noobdood Posted May 17, 2014 Author Share Posted May 17, 2014 did it Barands way but nothing happens. no delete, no errors or warnings, just a refresh. AndyPSV i haven't figured the jquery thing yet. not excellent at jquery yet Quote Link to comment Share on other sites More sharing options...
noobdood Posted May 17, 2014 Author Share Posted May 17, 2014 this worked for a member on this forum of course i edited accordingly but it doesn't delete it just refreshes. script: <script> $(document).ready(function(){ $("#del_event").click(function() { $(':checkbox:checked').each(function() { $.post('delete.php', { check: $(this).attr('value') } ); }); }); }); </script> delete.php <?php $del_id = $_POST['check']; $sqlqueer = mysql_query("DELETE FROM event WHERE event_id = $del_id") or die(mysql_error()); if($sqlqueer) { echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted May 17, 2014 Solution Share Posted May 17, 2014 This works for me. Note it uses mysqli and not mysql librarybut apart from that it is fundamentally the same code. <?php $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); // use your credentials error_reporting(-1); if(isset($_POST['del_event'])) { if(isset($_POST['check'])) { $del_ids = join(',', array_map('intval', $_POST['check'])); $sqlqueer = mysqli_query($db,"DELETE FROM event WHERE event_id IN ($del_ids)"); if(!$sqlqueer) { die ("Error in delete query"); } } } ?> <html> <head> <title>Sample</title> </head> <body> <form class="event-button-wrap" method="post" action=""> <div class="button-wrap"> <input type="submit" id="del_event" name="del_event" value="Delete Event"/> </div> <div class="event-list-table2" > <table class="event-table"> <thead> <tr> <th>Event Name</th> <th class="head">Event Date</th> <th class="head">Venue</th> </tr> </thead> <tbody class="tbody-event-list-table2"> <?php // require "connection.php"; $check = mysqli_query($db,"SELECT * FROM event"); if(mysqli_num_rows($check) > 0) { while($row = mysqli_fetch_array($check)) { $id = $row['event_id']; $name = $row['event_name']; $dstart = $row['start_date']; $venue = $row['event_venue']; echo "<tr> <td> <input type='checkbox' name='check[]' class='check' value='$id'>$name </td> <td>$dstart</td> <td>$venue</td> </tr>"; } } else { echo "<tr> <td colspan='3'>There Are No Events.</td> </tr>"; } ?> </tbody> </table> </div> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
noobdood Posted May 18, 2014 Author Share Posted May 18, 2014 thank you. it was actually my html structure that wasn't making it work. i don't know why but i had to rearrange the form and stuff. thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.