www.webdeveloper.com
Results 1 to 8 of 8

Thread: Javascript Alert Box Cancel

  1. #1
    Join Date
    Apr 2009
    Posts
    107

    Javascript Alert Box Cancel

    I am working on a PHP web application for my company and have run into a problem with Javascript. In a list I am displaying from a Mysql database, I have the option to delete certain entries. Before someone deletes an entry though, I want them to have an alert box pop up and asked then if they are sure. I have this simple javascript function in the head of the page, but whether cancel or ok are pressed, the page refreshes to the link. How can I fix it so that if you press cancel, nothing happens, other then the alert box goes away, but also allows the link to process if OK is pressed?

    HTML Code:
    <script type="text/javascript">
    function show_confirm()
    {
    var r=confirm("Press a button!");
    if (r==true)
      {
      alert("You pressed OK!");
      }
    else
      {
      alert("You pressed Cancel!");
      }
    }
    </script>

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    I assume your link has an onclick event. In the link you should place "return false" after the function call.

    Something like this

    HTML Code:
    <a href="example.com" onclick="show_confirm(); return false;">Link</a>
    If I am assuming the wrong thing or it doesn't work then show the code for your link.
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

  3. #3
    Join Date
    Apr 2009
    Posts
    107
    I did try placing
    HTML Code:
    onclick="show_confirm(); return false;">
    "return false;" adter the function call, but then it never allowed the link to process if OK was presses.

    To clarify, I have two buttons: Cancel and OK. If Cancel is press, I just want the alert box to disappear and no page refresh. If OK is press, I want the browser to navigate to the link.

  4. #4
    Join Date
    Mar 2007
    Posts
    946
    Can you show the code of how the function is being called?
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

  5. #5
    Join Date
    Apr 2009
    Posts
    107
    Here is the code for the function call.
    HTML Code:
    <td align="center"><a href="mra_test_admin_delete_q.php?id=<?php echo $test_id; ?>&test_proc_id=<?php echo $test_proc['id']; ?>&test_proc_num=<?php echo $test_proc_num; ?>" onclick="show_confirm(); return false;">X</a></td>

  6. #6
    Join Date
    Mar 2007
    Posts
    946
    Here I added return true/false in the if/else statements.
    HTML Code:
    <script type="text/javascript">
    function show_confirm()
    {
    var r=confirm("Press a button!");
    if (r==true)
      {
      alert("You pressed OK!");
      return true;
      }
    else
      {
      alert("You pressed Cancel!");
      return false;
      }
    }
    </script>
    Here I changed onclick to "return show_confirm();"
    PHP Code:
    <td align="center">
    <a href="mra_test_admin_delete_q.php?id=<?php echo $test_id?>&test_proc_id=<?php echo $test_proc['id']; ?>&test_proc_num=<?php echo $test_proc_num?>" onclick="return show_confirm();">X</a></td>
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Code:
    onclick="return show_confirm();"
    Code:
    function show_confirm()
    {
    var r=confirm("Press a button!");
    if (r==true)
      {
      return true;
      }
    else
      {
      return false;
      }
    }

  8. #8
    Join Date
    Apr 2009
    Posts
    107
    Bingo. That did it. Thanks skywalker 2208. Much appreciated.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles