0

I'm trying to pass an id for an item to be deleted through the URL after OK is clicked in a confirmation box, however nothing happens. This does works in IE but I'd like it to work with Chrome as well.

<script>
function dltCnfrm(id)
{
    var r=confirm("Delete this product?")
    if (r)
    {
        window.location.href = "http://localhost/e-com/index.php/product/delete_item/" + id;
    }
}
</script>

code inside html

foreach ($items as $res) {
$id = $res['id'];
.
.
<td><?php echo "<a href='' onclick='dltCnfrm($id)'>Delete</a>"; ?></td>

I have also tried several other methods including

location.assign();

adding return false; underneath the if statement

could someone try this using a Chrome browser? Chrome Version 24.0.1312.57 m

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
ethio
  • 539
  • 3
  • 10
  • 22

1 Answers1

1

I am betting the id is a string so you have an error

<td><?php echo "<a href='' onclick='dltCnfrm(\"$id\")'>Delete</a>"; ?></td>

You probably want to cancel the link click also.

<td><?php echo "<a href='' onclick='dltCnfrm(\"$id\"); return false'>Delete</a>"; ?></td>

BUT you should never never never do a delete action with a GET request. GET is for fetching data, POST is for updating data.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thanks, the solution works, but could you explain to me what you mean by "cancel the link click", by the way I'm using post not get. – ethio Feb 13 '13 at 22:25
  • 1
    By looking at the example, he means to stop the default `onclick` behavior (following the link) and execute the JS function only. See more here http://stackoverflow.com/questions/1070760/javascript-function-in-href-vs-onclick#answer-11348403 – kodeart Feb 13 '13 at 22:44
  • Where is the POST? Setting window.location is a GET request. There is no form posting going on. – epascarello Feb 14 '13 at 00:40