Using javascript custom dialog box before deleting a record. But does not know how to send the php value via js

Displaying N number of records from cart

<a href='delete.php?id=<?php echo $id; ?>'>Delete</a> 

This is the url for deleting the particular record.

Before deleting the record i wrote a custom js dialogue box.

But i does not know how to pass the php id value to delete.php

In the below code wherever i click it sends only id of the first record not the correct record.

function Confirm(title, msg, $true, $false, $link) { /*change*/   var $content = "<div class='dialog-ovelay'>" +     "<div class='dialog'><header>" +     " <h3> " + title + " </h3> " +     "<i class='fa fa-close'></i>" +     "</header>" +     "<div class='dialog-msg'>" +     " <p> " + msg + " </p> " +     "</div>" +     "<footer>" +     "<div class='controls' style='text-align:right'>" +     " <button class='button button-danger doAction'>" + $true + "</button> " +     " <button class='button button-default cancelAction'>" + $false + "</button> " +     "</div>" +     "</footer>" +     "</div>" +     "</div>";   $('body').prepend($content);   $('.doAction').click(function() {      $(this).parents('.dialog-ovelay').fadeOut(500, function() {       location.href = "duplicate_frame.php?id=<?php echo $id; ?>";       $(this).remove();     });   });   $('.cancelAction, .fa-close').click(function() {     $(this).parents('.dialog-ovelay').fadeOut(500, function() {       $(this).remove();     });   });  }  $('.linkdup').click(function() {   document.body.scrollTop = 0; // For Safari   document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera   Confirm('Are you sure you want to Duplicate Frame', 'One more frame will be added to Cart', 'Yes', 'No', ); /*change*/ });

Add Comment
1 Answer(s)

This is VERY BAD PRACTICE <a href='delete.php?id=<?php echo $id; ?>'>Delete</a> – one visit from a spider that ignores your coustom confirm will wipe your database

So you need to do this:

<button type="button" class="delete" data-id="<?php echo $id; ?>">Delete</button> 

Then you can do

let currentLink = "" $('.delete').on('click',function() {   const id  = $(this).data("id");   currentLink = "delete.php?id="+id;   document.body.scrollTop = 0; // For Safari   document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera   Confirm('Are you sure you want to delete item','This will delete '+id, 'Yes', 'No');  }); 

and have

$('.doAction').click(function() {   $(this).parents('.dialog-ovelay').fadeOut(500, function() {     location.href = currentLink;     $(this).remove(); // does this even execute?   }); }); 
Answered on July 15, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.