0

I have a contact form. Now when I need to delete a record, I need a confirmation box to ask for user's confirmation. I've achieved this, so far so good. But the record gets deleted for both OK and Cancel buttons.

My code is as follows:

<td><a href="edit.php?id=<?php $_SESSION["name"] = ""; $_REQUEST["id"]=$row["first_name"]; echo $row["first_name"]; $_SESSION["name"]=$row["first_name"]; ?>">Edit</a> / <a href="delete.php?id=<?php echo $row["first_name"]; ?>" onclick="check()"> Delete </a></td>

<script>
    function check(){
        if(confirm("Are you sure you want to delete?")){
            window.location.href = "delete.php?id=<?php echo $row["first_name"]; ?>";
            return true;
        }
        else{
            header("Location: http://localhost/test/display.php?show_details=Show+Details");
            return false;
        }
    }
</script> 

What could I do to delete the records only after clicking OK button in the confirmation box and return to display.php(same page) on clicking Cancel?

It should navigate to delete.php only on clicking OK and stay in display.php on clicking Cancel.

I'm new to PHP.. Help please...

feeela
  • 29,399
  • 7
  • 59
  • 71
Lublaut
  • 339
  • 4
  • 11
  • you have to prevent the default beahviour when you click the link. Check [preventDefault](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) – Sfili_81 Aug 29 '18 at 10:07
  • Where are OK and Cancel buttons? – Zain Farooq Aug 29 '18 at 10:07
  • 1
    you dont need to add href attribute (just put # in href) as you are redirecting the window in javascript – Viswanath Polaki Aug 29 '18 at 10:09
  • 1
    @ViswanathPolaki The other way around would be much more accessible though: Use a normal HTML anchor with a link in the `href`-attribute that would delete the item. Add the check via JS and re-use the content of the `href`-attribute in JS instead of hardcoding the link here again. The benefit is, that the app itself is working, no matter if JS is available or not. The JS layer only adds some UX sugar. – feeela Aug 29 '18 at 10:27
  • not adding `href` is fine, but i need to look at the link when i hove over the `delete` option, i.e., I want the `delete` option to be a link – Lublaut Aug 29 '18 at 10:27
  • This is not a PHP issue, but a JavaScript issue… – feeela Aug 29 '18 at 10:28
  • and how would i clear it @feeela – Lublaut Aug 29 '18 at 10:29
  • There's no `header` function in `JavaScript`, I'll assume that you meant `location.href` or you just implemented a function that you named `header`. About your issue, you don't need the `href` attribute in the `a` tag as you're redirecting using `JavaScript`. So, in the click handler you need to prevent the default behaviour(by calling `preventDefault()` on the event argument). – ThS Aug 29 '18 at 10:35

1 Answers1

0

An HTML anchor has a default action for clicks on it: open a link. Now if you add another click handler in JavaScript, you will have to prevent the default action for that element and thus prevent the link from being opened.

To prevent the default event action from within an inline event handler (one that is setup using the attribute onclick in contrast to use addEventListener() in JS) you must return false in that event handler.

See also: How to prevent default event handling in an onclick method?

The following two snippets fix that issue and are also re-using the href-attribute, so it must not be hardcoded in JS again.

variant 1:

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <a href="delete.php?id=123" onclick="return check(this)">Delete</a>
</p>

<script>
    function check(anchor) {
        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }

        /* Return FALSE to prevent the default link action */
        return false;
    }
</script>

variant 2:

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <!-- Return FALSE to prevent the default link action -->
    <a href="delete.php?id=123" onclick="check(this); return false;">Delete</a>
</p>

<script>
    function check(anchor) {
        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }
    }
</script>

variant 3A:

Not returning false, but using event.preventDefault(). (Inspired by the comment of @ths)

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <a href="delete.php?id=123" onclick="check(event, this)">Delete</a>
</p>

<script>
    function check(event, anchor) {
        /* Prevent the default link action */
        event.preventDefault();

        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }
    }
</script>

variant 3B:

Not returning false, but using event.preventDefault(). (Inspired by the comment of @ths)

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <a href="delete.php?id=123" onclick="event.preventDefault(); check(this);">Delete</a>
</p>

<script>
    function check(anchor) {
        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }
    }
</script>
feeela
  • 29,399
  • 7
  • 59
  • 71
  • I tried both the variants. the `delete` option does not show as a link. And then I couldn't delete the records!! – Lublaut Aug 29 '18 at 10:46
  • @Lublaut What do you mean with “does not show as a link”? The anchor is displayed and after clicking “OK” in the confirmation box, the URL properly redirected to the delete URL. I stripped out the PHP for the sake of simplification in the examples. Make sure to add the correct ID again using PHP. – feeela Aug 29 '18 at 10:48
  • What is the `event` in **variant 3A** !? – Lublaut Aug 29 '18 at 10:50
  • @Lublaut It is the JavaScript event object. Please refer to [MDN: DOM on-event handlers](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers) and [MDN: The Event interface](https://developer.mozilla.org/en-US/docs/Web/API/Event) – feeela Aug 29 '18 at 10:52
  • I want the delete option to look like a link like the edit option – Lublaut Aug 29 '18 at 10:54
  • @Lublaut It does already by default. If does not look like a link you probably have some CSS included in you page that affects the visual appearance. With onyl the code from the examples above in an HTML document, the edit and delete links do look visually identical. – feeela Aug 29 '18 at 10:56
  • I now looks like the records are not deleted. Tried all the variants, and the records are not deleted – Lublaut Aug 29 '18 at 10:57
  • @Lublaut That is another issue then. Have you replaced the static `id=123` with your PHP code (`id=`) again? – feeela Aug 29 '18 at 10:58
  • Yes i have replaced it – Lublaut Aug 29 '18 at 10:59