MrFoh
MrFoh

Reputation: 2701

Javascript confirm delete

Am creating a simple cms and i have the following

<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="delete_confirm()" />

<tbody>
  <?php foreach ($articles as $article): ?>
  <tr>
    <td align="center">
    <input id="article_id"name="article[]" type="checkbox" value="<?php echo $article->id; ?>" /></td>
    <td align="center"><?php echo anchor('admin/articles/edit/'.$article->id,$article->title); ?></td>
    <td align="center"><?php echo $article->author; ?></td>
    <td align="center"><?php echo $article->category; ?></td>
    <td align="center"><?php published($article->post_status); ?></td>
    <td align="center"><?php featured($article->featured); ?></td>
    <td align="center"><?php echo $article->views; ?></td>
    <td align="center"><?php echo $article->post_date; ?></td>
  </tr>
  <?php endforeach;?>
  </tbody>

The above code is wrapped in a form tag

This my javascript code

function delete_confirm() {
    var msg = confirm('Are you sure you want to delete the selected article(s)');
    if(msg == false) {
        return false;
    }
}

This is delete function in my controller

function articles_delete() {
        //Load Model
        $this->load->model("article_model");
        //Assign article id to variable
        $checkbox = $_POST['article'];
        //loop through selected checkbox
        for($i = 0; $i < count($checkbox); $i++) {
            //Call delete method of the article model
            $this->article_model->delete($checkbox[$i]);
        }
        $this->session->set_flashdata("message","Article(s) Deleted");
        redirect("admin/articles","refresh");
     }

But when cancel is clicked in the confirm dialog the form still is still submited and the selected article deleted. Please advice on what to do

Upvotes: 6

Views: 5713

Answers (7)

evan
evan

Reputation: 4297

Try changing

<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="delete_confirm()" />

to

<input type="submit" value="delete" title="Delete Aricle(s)" id="delete_btn" onclick="return delete_confirm();" />

You're running the delete_confirm() function inside of the onclick function. you need to return false from the onclick() function itself.

Upvotes: 6

meltuhamy
meltuhamy

Reputation: 3483

Try using AJAX to call the PHP script from within Javascript.

This means set the input button type to button <input type="button" /> and change the onclick such that if cancel is pressed, return false (like what you did) or if OK is pressed, run the AJAX script and redirect the page if required...

You could write the AJAX server-side (PHP) script in a seperate file, and send in the paramters using $_GET.

Upvotes: 2

nosbor
nosbor

Reputation: 2999

Maybe try to use:

<a href="javascript: if(confirm('Relly?')) { document.getElementById('your_form').action='php.php'; document.getElementById('your_form').submit(); } ">Delete</a>

Upvotes: 0

Ray Toal
Ray Toal

Reputation: 88378

You want the delete confirm function to be your onsubmit attribute of the form. Having it on the input element is not enough.

Upvotes: 0

Lukas Knuth
Lukas Knuth

Reputation: 25755

What you'll want to do is something like this:

<form onsubmit="return delete_confirm();" ...>
  <input ...>
</form>

When the form is submitted via the type="submit"-button, your delete_confirm()-function is called. If it returns true, the form is submitted. If it returns false, it's not.

Upvotes: 2

Eugo
Eugo

Reputation: 1

delete_confirm() must always return false in your case.

Upvotes: 0

joakimdahlstrom
joakimdahlstrom

Reputation: 1595

try onSubmit instead of onClick.

Upvotes: 2

Related Questions