Reputation: 352
To illustrate my problem description, there is a list to which one or more tasks can be attached.
I have devised thing as below,
<?php
try
{
$list_id = $_GET['id'];
//Instantiate Database object
$database = new Database;
//check if task exist for the list to be deleted
$database->query('SELECT * FROM tasks WHERE list_id = :id');
$database->bind(':id',$list_id);
$database->single();
$rc = $database->rowCount();
if($rc > 0)
{
?>
<script type="text/javascript">
if(confirm('The list has one or more task attached to it. Deleting the list will also delete the attached tasks. Do you want to proceed ?'))
{
//prepare and execute delete tasks
$database->query('DELETE FROM tasks WHERE list_id = :id');
$database->bind(':id',$list_id);
$database->execute();
//prepare and execute delete lists
$database->query('DELETE FROM lists WHERE id = :id');
$database->bind(':id',$list_id);
$database->execute();
if($database->rowCount() > 0)
{
header("Location:index.php?msg=listdeleted");
}
}
</script>
<?php
}
}
catch(Exception $e)
{
echo '<p>'.$e->getMessage().'</p>';
}
?>
The problem is that - The code does nothing. No error. Simply nothing. I did some googling and found out that most people are suggesting ajax call to do that. I do not know ajax. So, to do this tiny bit of thing, I would have to devote some considerable amount of time now. So, I need the following 2 things -
Upvotes: 1
Views: 326
Reputation: 22
Yes, as the guys said, ajax will be one nice way to do this.
The confirm alert it's working? Inside of your js function, you can't put the php code without the "< ? php..?>" between then as you did.
Upvotes: 0
Reputation: 3027
Either
a) learn to use ajax, as it is the most obvious solution. Once you have learned it you will find it invaluable
or, if you really haven't the time
b) use your JS 'confirm' to redirect the user to a new PHP page, passing the ID of the record that you want to delete in the URL of the page redirect. On that new PHP page, process your delete and then use a PHP redirect to bring the user back to your original page.
Go for option (a)...
Upvotes: 2