0

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*/
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Mohamed
  • 23
  • 1
  • 9
  • You need to parameterise `` in `location.href = "duplicate_frame.php?id=";` I suggest you use data-attributes on the link that opens the dialog – mplungjan Jul 15 '20 at 11:05
  • what elements are you using ( class `.linkdup` ) - can you show examples? Also - don't hardcode the php variable `$id` in the javascript function - better to obtain the ID from the HTML element somehow – Professor Abronsius Jul 15 '20 at 11:05
  • Does this answer your question? [How do I pass JavaScript variables to PHP?](https://stackoverflow.com/questions/1917576/how-do-i-pass-javascript-variables-to-php) – kelvin Jul 15 '20 at 11:11
  • Also have google spider your `Delete` and your database is gone – mplungjan Jul 15 '20 at 11:28

1 Answers1

1

This is VERY BAD PRACTICE <a href='delete.php?id=<?php echo $id; ?>'>Delete</a> - one visit from a spider that ignores your custom 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?
  });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236