SubhasisM
SubhasisM

Reputation: 352

Execute php database code inside javascript

To illustrate my problem description, there is a list to which one or more tasks can be attached.

  1. 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.
  2. 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.
  3. 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 -

  1. 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
  2. From the code I pasted above, can anyone suggest any better approach to what I am trying to accomplish?

Upvotes: 1

Views: 326

Answers (2)

Luiz Pillon
Luiz Pillon

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

MaggsWeb
MaggsWeb

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

Related Questions