lalnfl Posted August 12, 2010 Share Posted August 12, 2010 Okay I am working on a mail script and my only problem is, is that when you select mutiple delete boxes, it only deletes one. How do I get it to delete all of the checked boxes? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 12, 2010 Share Posted August 12, 2010 <form method="post"> box 1 <input type="checkbox" name="box[]" value="1"><br /> box 2 <input type="checkbox" name="box[]" value="2"><br /> box 3 <input type="checkbox" name="box[]" value="3"><br /> <input type="submit" name="submit" value="View Checked Boxes"> </form> <?php if(isset($_POST['submit'])){ $box = $_POST['box']; // this will be an array if(count($box) > 0){ foreach($box AS $id){ echo $id . "<br />\n"; } } } ?> You give each checkbox the same name with the brackets on the end of it, so in my case box[]. For each checkbox selected it will add that value to an array. The rest is just grabbing the values in that array. Quote Link to comment Share on other sites More sharing options...
shlumph Posted August 12, 2010 Share Posted August 12, 2010 What do you mean by delete all the checkboxes? After you submit the form, the checkboxes don't show up again? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 12, 2010 Share Posted August 12, 2010 No, he's talking about like an inbox system or something related. The user would select multiple messages and click delete, all of the messages that were selected would be deleted when the form processes. Quote Link to comment Share on other sites More sharing options...
lalnfl Posted August 12, 2010 Author Share Posted August 12, 2010 <form method="post"> box 1 <input type="checkbox" name="box[]" value="1"><br /> box 2 <input type="checkbox" name="box[]" value="2"><br /> box 3 <input type="checkbox" name="box[]" value="3"><br /> <input type="submit" name="submit" value="View Checked Boxes"> </form> <?php if(isset($_POST['submit'])){ $box = $_POST['box']; // this will be an array if(count($box) > 0){ foreach($box AS $id){ echo $id . "<br />\n"; } } } ?> You give each checkbox the same name with the brackets on the end of it, so in my case box[]. For each checkbox selected it will add that value to an array. The rest is just grabbing the values in that array. So what if I have if statements where you have echo $id? How would I format it so that the if statements would process? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 12, 2010 Share Posted August 12, 2010 Like if statements to check whether or not the "message ID" exists and belongs to that user? <form method="post"> box 1 <input type="checkbox" name="box[]" value="1"><br /> box 2 <input type="checkbox" name="box[]" value="2"><br /> box 3 <input type="checkbox" name="box[]" value="3"><br /> <input type="submit" name="submit" value="View Checked Boxes"> </form> <?php if(isset($_POST['submit'])){ $box = $_POST['box']; // this will be an array if(count($box) > 0){ foreach($box AS $id){ $sql = "SELECT * FROM `messages` WHERE `id`='".mysql_real_escape_string($id)."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ // msg exists $row = mysql_fetch_assoc($res); if($row['uid'] == $_SESSION['uid']){ // msg belongs to logged in user $sql2 = "DELETE FROM `messages` WHERE `id`='".mysql_real_escape_string($id)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); // msg deleted }else { // msg doesn't below to user, nothing is deleted // you can put your error msgs in here if you want } }else { // msg doesn't exist, nothing is deleted // you can put your error msgs in here if you want } } } } ?> Quote Link to comment Share on other sites More sharing options...
lalnfl Posted August 12, 2010 Author Share Posted August 12, 2010 No, I am talking about if statements that delete the mail. How would you do that? Quote Link to comment Share on other sites More sharing options...
Alex Posted August 12, 2010 Share Posted August 12, 2010 That's what mgallforever's solution would do. But I would suggest doing it another way. There's no need for all those loops and tons of queries. It can be simplified: $sql = "DELETE FROM messages WHERE id IN (" . implode(", ", $_POST['box']) . ") AND uid = " . $_SESSION['uid']; Quote Link to comment Share on other sites More sharing options...
lalnfl Posted August 12, 2010 Author Share Posted August 12, 2010 Okay I finally figured it out, but now I am getting an error message when I just try to delete 1 message. Do I need to do an else statement outside of the foreach statement? Quote Link to comment Share on other sites More sharing options...
shlumph Posted August 13, 2010 Share Posted August 13, 2010 What's your error message? I believe that you'll only want to implode if count($_POST['box']) > 0 Quote Link to comment Share on other sites More sharing options...
lalnfl Posted August 13, 2010 Author Share Posted August 13, 2010 Thanks for the help guys I figured it out. Quote Link to comment Share on other sites More sharing options...
YourNameHere Posted August 13, 2010 Share Posted August 13, 2010 Thanks for the help guys I figured it out. How did you figure it out? What was your solution? 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.