Reputation: 1475
OK folks,
I am trying to display the contents of a database table along with a REMOVE or PUSH button. The REMOVE button will remove said item from the database. The PUSH button will push the item into another database, then FLAG said item by setting a variable pushed to true.
I have the connection and the while loop echoing each item in the database along with a REMOVE button and a PUSH button. the echo looks like this:
echo '<form method='post' name='$ID' action='$_PHP_SELF'>
<tr>
<td width='5%' align='center'>$ID</td>
<td width='75%'>$content</td>
<td width='10%' align='center'><input type='submit' name='remove' id='remove' value='REMOVE'></td>
<td width='10%' align='center'><input type='submit' name='push' id='push' value='PUSH'></td>
</tr>
</form>';
The problem I'm running into, is how I can set a simple submit code that will check with form and either push or remove that specific item. If/Else or Switch statements won't work because I'm expecting dozens of database entries and I want the submit code itself to be dynamic as well.
I started out with:
if(isset($_POST['push'])){
echo "PUSH ".$ID." TO DATABASE";
}
if(isset($_POST['remove'])){
echo "REMOVE ".$ID." FROM DATABASE";
}
but both of these are only returning the last $ID variable.
thanks in advance.
Upvotes: 2
Views: 1531
Reputation: 2017
An easy solution would be to change your HTML output to have two forms, with one submit for each. You could keep your PHP exactly the same.
EDIT: In order to dynamically get the ID of the button pushed, then add a hidden data field with the ID in it, as below:
<tr>
<td width='5%' align='center'>$ID</td>
<td width='75%'>$content</td>
<td width='10%' align='center'>
<form method='post' action='$_PHP_SELF'>
<input type='hidden' value='$ID' name='id'>
<input type='submit' name='remove' id='remove' value='REMOVE'>
</form>
</td>
<td width='10%' align='center'>
<form method='post' name='$ID' action='$_PHP_SELF'>
<input type='hidden' value='$ID' name='id'>
<input type='submit' name='push' id='push' value='PUSH'>
</form>
</td>
</tr>
Then, your PHP would be:
$ID=mysql_real_escape_string($_POST['id']); //Encapsulate in this function to prevent bad IDs causing SQL injections.
if(isset($_POST['push'])){
echo "PUSH ".$ID." TO DATABASE";
}
if(isset($_POST['remove'])){
echo "REMOVE ".$ID." FROM DATABASE";
}
Upvotes: 2
Reputation: 668
Follow this tutorial for creating a checkbox system and php processor. Php Checkboxes
You can do it one of two ways. If you want the user to be able to delete several items at a time with one link you can do it like this.
Have the user select via checkbox as many rows as they want to. Then hit the "REMOVE" or "PUSH" button that sends it to another page with processes each row.
The other way would be to have a REMOVE and PUSH button next to each row which will remove or push a single row at a time. I would recommend the first approach. If you follow that guide its pretty straight forward.
Upvotes: 0
Reputation: 31471
Make each relevant row it's own form, with it's own two push buttons. They can all submit to the same page. Also, keep the ID in a hidden field (and verify server side), not as the form name.
Upvotes: 2