SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Problem with confirmation button

    Hi,
    I've a problem with easy confirmation script. It goes to delete.php either you hit the ok or cancel buttons.

    Code:
    <script type='text/javascript'>
    <!--
    	function confirmDelete() {
    		return confirm("Are you sure you want to remove the user from database?");
    	}
    //-->
    </script>
    <?php
    	echo "<a href='delete.php?user_id=" .$row['user_id']. "'><img src='images/delete.png' onclick='confirmDelete()' title='delete user' /></a>";
    ?>
    Thanks for your help in advance

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The returned value from the confirmDelete function needs to be returned to the onclick event.

    When you use an inline attribute to assign a scripting event,

    Code html4strict:
    <... onclick='confirmDelete()'>

    This is the function that gets assigned to that event:

    Code javascript:
    function () {
        confirmDelete()
    }

    There are better ways to deal with that. For example, if you used scripting to assign confirmDelete to the event of the element, there is no outer function wrapper that needs to be dealt with, so the return from confirmDelete achieve exactly what needs to be achieved:

    Code javascript:
    var deleteUser = document.getElementById('delete');
    deleteUser.onclick = confirmDelete;

    But a few other preparation pieces need to be performed before you can start doing things so easily.

    So, in order for you to have the easiest solution (which may not be the best) you can just return to the event attribute the result of the confirmDelete function that you are calling.

    Code html4strict:
    <... onclick="return confirmDelete()">
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •