Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 669
MrFoh Avatar asked Jul 16 '11 20:07

MrFoh


People also ask

Are you sure delete message in JavaScript?

You can easily display a confirmation dialog using Window confirm() method in the client-side. The confirm() method show a dialog box with a message and two buttons (OK and Cancel). This method returns true, if the user clicks OK, otherwise false.

How do I change my confirmation before delete?

On the desktop, navigate to the "Recycle Bin" folder. Right-click on the Recycle Bin folder and click on the "Properties" option. "Recycle Bin Properties" window will appear on the screen. Click (select) on the "Display delete confirmation dialog" option and click on the "Apply" button to proceed.

What is confirm in JavaScript?

Javascript | Window confirm() Method The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed. Syntax: confirm(message)

How do you make a confirmation pop up in HTML?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .


2 Answers

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.

like image 160
evan Avatar answered Oct 24 '22 04:10

evan


try onSubmit instead of onClick.

like image 40
joakimdahlstrom Avatar answered Oct 24 '22 04:10

joakimdahlstrom