To illustrate my problem description, there is a list to which one or more tasks can be attached.
- when user tries to delete the list, a php code would check mysql 'tasks' table to see if there is any task(s) attached to the list being deleted.
- if there is/are task(s), user would be prompted a popup, saying that deleting the list would also delete all the attached tasks, asking for confirmation for the same.
- on OK, would proceed and delete all tasks, then delete the list .... on Cancel, would do nothing.
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 -
- Could you people please confirm me if ajax is the only possible solution here? I would only go to ajax if no other way left to execute php as shown above
- From the code I pasted above, can anyone suggest any better approach to what I am trying to accomplish?