Checkboxes

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\
";
echo "<thead>";
echo "\	<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\
";
echo "</thead>";


if($result->num_rows){
while ($row = $result->fetch_array()){
$RowCount ++;
$row_color = ($RowCount % 2) ? $color1 : $color2;
echo "\	<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.

I cannot see where $_POST[‘delete’] is being formed, so that could be a reason.

If you have Teamviewer installed PM me your connection details within 60 minutes of this post and I’ll hop over to your desktop and debug this with you.

  1. You create a new form for each entry? So each form has only one checkbox?
  2. Your forms have no submit button?
  3. You give the checkbox the value of $rows[‘id’], and that should be (I think) $row[‘id’]

If you have Teamviewer installed PM me your connection details within 60 minutes of this post and I’ll hop over to your desktop and debug this with you.

Sorry tangoforce I don’t have Teamviewer installed but it would be best if you could just help me fix my codes in your reply =)

  1. You create a new form for each entry? So each form has only one checkbox?
  1. Your forms have no submit button?

  2. You give the checkbox the value of $rows[‘id’], and that should be (I think) $row[‘id’]

  3. Yes since i am using a while loop, so there is a checkbox near each entry respectively as usual

  4. There’s one delete (submit) button above the table

  5. Yes it should be $row[‘id’], good point. But still the same warning pops up

Thanks

A checkbox near each entry, ok. But all checkboxes should be part of one single form.

  1. There’s one delete (submit) button above the table

Above the table, and in another form. That’s why you get the error. Clicking that button, you send that form and that form contains only the button. No other data (since all checkboxes have their own forms that will never be sent).
Put the button and all checkboxes in one form and see what happens.

So what you are saying is to put that delete button inside the while loop as well?

And I am confused about the putting all the checkboxes into one single form… So you are saying I take out the

<td><input name=\\"checkbox[]\\" type=\\"checkbox\\" id=\\"checkbox[]\\" value=\\"{$rows['id']} \\"></td>

OUT of the while loop?

I just want to select multiple entries by clicking the corresponding checkboxes then click a single delete button to do the job

Sorry I am kind of lost here, 'cause I am new to checkboxes…

hope you can clarify it a bit more

Thats why I offered assistance. TV is a free download if you’d spent 20 seconds to google it :rolleyes:

Nevermind, you made it clear you’d prefer to spend hours waiting for replies etc so I’ll leave you to it. FYI, most people I’ve offered that kind of help to have jumped at it.

You could do that, it would give you a delete button for each entry.
But it’s not what I had in mind.

And I am confused about the putting all the checkboxes into one single form… So you are saying I take out the

<td><input name=\\"checkbox[]\\" type=\\"checkbox\\" id=\\"checkbox[]\\" value=\\"{$rows['id']} \\"></td>

OUT of the while loop?

No. That line must be inside the loop. The <form> and </form> should not be.

I just want to select multiple entries by clicking the corresponding checkboxes then click a single delete button to do the job

Sorry I am kind of lost here, 'cause I am new to checkboxes…

hope you can clarify it a bit more

No worries :slight_smile: I’m trying to explain it to you, instead of just giving you the solution, so you can understand how it works.

So putting all in one form it would become:

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>

echo "<table class=\\"sortable\\" id=\\"query_quick1\\" width=\\"100%\\" >\\r\
";
echo "<thead>";
echo "\	<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\
";
echo "</thead>";


if($result->num_rows){
  while ($row = $result->fetch_array()){
    $RowCount ++;
    $row_color = ($RowCount % 2) ? $color1 : $color2;
    echo "\	<tr class=\\"$row_color\\" >

    <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>
</tr>";
}
}
echo "</table>";
echo "</form>";


As you can see, I deleted the two </form>'s you had, and added a new one at the end. I also deleted the hidden field with the id. It’s useless, because you get that value from $_POST[‘checkbox’].

THANKS guido2004

you are called mentor for a reason :cool: