Reputation: 679
Hi I'm new to trying to do a delete button using PHP & DABL.
When I click the delete button it deletes it from the DB but still shows on the page.
If I refresh the page or click the button again it removes it.
I know i'm doing something wrong but not sure what.
Many Thanks
Mark
<?php
require_once('includes/header.php');
$loginTypes = LoginTypes::getAll();
<ul>
<?php foreach ($loginTypes as $loginType){?>
<li><?php echo $loginType->type; $LoginTypeId = $loginType->login_type_id; ?>
<form action ="<?php $_SERVER['PHP_SELF']; ?>" method='post' >
<input type='hidden' name='<?php echo $LoginTypeId; ?>' id='<?php echo $LoginTypeId; ?>' />
<input type='submit' name='submit_<?php echo $LoginTypeId; ?>' id="submit_<?php echo $LoginTypeId; ?>" value='delete' />
</form>
</li>
<?php if(isset($_POST["submit_$LoginTypeId"])){
$delete = LoginTypes::retrieveByPK("$LoginTypeId");
$delete->delete();
}} ?>
</ul>
<?php require_once('includes/footer.php'); ?>
Upvotes: 0
Views: 652
Reputation: 6346
A slightly modified version of Alex Ciminian's code.
Main changes are where you are dealing with the POST variable. This should work, but obviously I can't really test it without having your logintypes class at my disposal.
Since they are all in different forms you don't need to use a unique name for the submit button (or the delete id hidden field). I'd still be tempted to improve the code by putting it all in one form and have checkboxes to mark which ones you want to delete, but this should suffice for now.
<?php
require_once('includes/header.php');
if(isset($_POST['deleteSubmit'])) {
$delete = LoginTypes::retrieveByPK($_POST['LoginTypeId']);
$delete->delete();
}
$loginTypes = LoginTypes::getAll();
?>
<ul>
<?php
foreach ($loginTypes as $loginType){ ?>
<li><?php echo $loginType->type; $LoginTypeId = $loginType->login_type_id; ?>
<form action ="<?php $_SERVER['PHP_SELF']; ?>" method='post' >
<input type='hidden' name='LoginTypeId' value='<?php echo $LoginTypeId; ?>' id='<?php echo $LoginTypeId; ?>' />
<input type='submit' name='deleteSubmit' id="submit_<?php echo $LoginTypeId; ?>" value='delete' />
</form>
</li>
<?php } ?>
</ul>
<?php require_once('includes/footer.php'); ?>
Upvotes: 1
Reputation: 11498
Because you display the item before you actually delete it. Put the if
before the foreach
.
Try this:
<?php
require_once('includes/header.php');
?>
<ul>
<?php
if(isset($_POST["submit_$LoginTypeId"])) {
$delete = LoginTypes::retrieveByPK("$LoginTypeId");
$delete->delete();
}
$loginTypes = LoginTypes::getAll();
foreach ($loginTypes as $loginType){ ?>
<li><?php echo $loginType->type; $LoginTypeId = $loginType->login_type_id; ?>
<form action ="<?php $_SERVER['PHP_SELF']; ?>" method='post' >
<input type='hidden' name='<?php echo $LoginTypeId; ?>' id='<?php echo $LoginTypeId; ?>' />
<input type='submit' name='submit_<?php echo $LoginTypeId; ?>' id="submit_<?php echo $LoginTypeId; ?>" value='delete' />
</form>
</li>
<?php } ?>
</ul>
<?php require_once('includes/footer.php'); ?>
Upvotes: 2
Reputation: 119
You should perform delete action before
$loginTypes = LoginTypes::getAll();
Upvotes: 0