Javascript confirm delete

We create 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 is 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 the 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 the cancel button is pressed in the confirmation dialog box, the form is still saved and the selected article is deleted. Please advice on what to do.

+6
source share
7 answers

Try to change

<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 use the delete_confirm () function inside the onclick function. you need to return false from the onclick () function itself.

+6
source

try onSubmit instead of onClick .

+2
source

What you want to do is something like this:

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

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

+2
source

Try using AJAX to call a PHP script from Javascript.

This means that for the enter button, enter the <input type="button" /> and change the onclick value so that if cancel is pressed, return false (for example, what you did) or click OK, run the AJAX script and redirect the page if required ...

You can write the server side of the AJAX (PHP) script in a separate file and send the parameters using $ _GET.

+2
source

delete_confirm() should always return false in your case.

0
source

You want the deletion confirmation function to be your onsubmit attribute of the form. It is not enough to have it on the input element.

0
source

Perhaps try using:

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

Source: https://habr.com/ru/post/892912/


All Articles