• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 737
  • Last Modified:

return confirm and jquery bind

I have a link that performs a jquery bind but I want to have the user confirm first. When I use an onclick return confirm in the a href the bind event is still called.

<a href=\"folders.php?action=deletesub&id=$row[id]&mid=$id\" id=\"delsub$row[id]\" onclick=\"return confirm('Are you sure?')\"><img src=\"x.png\" height=\"32\" border=\"0\"></a>

Open in new window


                                $("#delsub<?=$row['id'];?>").bind('click',function(event){
                                        event.preventDefault();
                                        $.get(this.href,{},function(response){
                                                $('#action_window2').html(response)
                                        })
                                });

Open in new window

0
Robert Saylor
Asked:
Robert Saylor
  • 3
1 Solution
 
Jim RiddlesPrepress/OMS SpecialistCommented:
Instead of using the return(confirm()), I would use a JQuery UI modal dialog box and execute your action on a confirm click there.  That way you stay with JQuery.  Also, you will need to add a link to the JQuery UI library to your page:

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>

$("#delsub<?=$row['id'];?>").bind('click',function(event){
  var curAnchor = $(this);
  event.preventDefault();
  $("#confirmDialog").dialog({
    autoOpen: false,
    height: 200,
    width: 350,
    modal: true,
    buttons: {
      "Ok": function () {
        $.get(curAnchor.attr('href'),{},function(response){
          $('#action_window2').html(response)
        });
        },
      Cancel: function () {
        $(this).dialog("close");
      }
    },
    close: function () {}
});

Then add the following HTML code to your page:

<div id="confirmDialog">Are you sure?</div>

Finally, remove the onClick event from your anchor link.

One final note:  It looks like you might be building a separate click event for each unique link from your PHP page.  If that is the case, I would suggest changing your approach and giving each anchor link the same class.  Then the above code could be altered to write one function to catch every link, potentially reducing your page size considerably.  If you give each anchor link a class of "delsub" then you could alter the first line to the following:

$(".delsub").bind('click',function(event){

or the following if your content is generated dynamically:

$(document).bind('click',".delsub",function(event){
0
 
Robert SaylorSenior DeveloperAuthor Commented:
Thanks! I will give that a try tonight. I think you have given me enough where I should be able to figure it out.
0
 
Robert SaylorSenior DeveloperAuthor Commented:
I get this error when I click on the link to perform the delete function of my php class file.

$(...).dialog is not a function

Here is my function that I am using.
        public function adm_folder_list() {

                ?>
                <?php
                print "<div id=\"confirmDialog\">Are you sure?</div>";

                print "<hr>";
                $sql = "SELECT * FROM `folders` ORDER BY `folder` ASC";
                $result = $this->new_mysql($sql);
                while ($row = $result->fetch_assoc()) {
                        print "<a href=\"javascript:void(0)\" onclick=\"toggledisplay('sublist_$row[id]')\"><img src=\"folder.png\" height=\"32\" align=\"left\" border=\"0\"></a>&nbsp;&nbsp;
                                <a href=\"folders.php?action=delete&id=$row[id]\" id=\"del$row[id]\">
                                <img src=\"x.png\" height=\"32\" border=\"0\"></a>
                                <div id=\"update_main_$row[id]\" style=\"display:inline\">$row[folder] <a href=\"folders.php?action=edit_main&id=$row[id]\" id=\"update_$row[id]\">Edit</a></div><br><br>";
                                ?>
                                <script>
                                $("#del<?=$row['id'];?>").bind('click',function(event){
                                        var curAnchor = $(this);
                                        event.preventDefault();
                                        $("#confirmDialog").dialog({
                                                autoOpen: false,
                                                height: 200,
                                                width: 350,
                                                modal: true,
                                                buttons: {
                                                        "Ok": function () {
                                                                $.get(curAnchor.attr('href'),{},function(response){
                                                                        $('#action_window2').html(response)
                                                                });
                                                        },
                                                        Cancel: function () {
                                                                $(this).dialog("close");
                                                        }
                                                },
                                                close: function () {}
                                        })
                                });
                                </script>
                                <script>
                                /*
                                $("#del<?=$row['id'];?>").bind('click',function(event){
                                        event.preventDefault();
                                        $.get(this.href,{},function(response){
                                                $('#action_window2').html(response)
                                        })
                                });
                                */
                                $("#update_<?=$row['id'];?>").bind('click',function(event){
                                        event.preventDefault();
                                        $.get(this.href,{},function(response){
                                                $('#update_main_<?=$row['id'];?>').html(response)
                                        })
                                });
                                </script>
                                <?php
                                // list subs
                                $this->adm_subfolder_list($row['id']);
                }
        }

Open in new window



Here is the JS in the head area
<link href="jquery-ui-1.10.3/themes/base/jquery.ui.all.css" rel="stylesheet">
<script src="jquery-ui-1.10.3/jquery-1.9.1.js">
<script src="jquery-ui-1.10.3/ui/jquery.ui.core.js">
<script src="jquery-ui-1.10.3/ui/jquery.ui.widget.js">
<script src="jquery-ui-1.10.3/ui/jquery.ui.datepicker.js">
<link href="jquery-ui-1.10.3/demos/demos.css" rel="stylesheet">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<script src="tinymce/tinymce.min.js" type="text/javascript">
<script type="text/javascript">
<link type="text/css" rel="stylesheet" href="http://fonts.googleapis.com/css?family=Arvo:400">
<link type="text/css" rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lato:400,700">
<link href="css/style.css" rel="stylesheet">
<link href="css/responsive.css" rel="stylesheet">
<link href="menu.css" type="text/css" rel="stylesheet">
<script src="js/modernizr.js">
<script src="http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&count=100&include_rts=true&exclude_replies=true&screen_name=ansimuz&callback=Chirp.callback1">

Open in new window

0
 
Robert SaylorSenior DeveloperAuthor Commented:
I ended up using jquery to do a 2 step process sending the user to a confirmation screen then going back to the dynamic data based on there selection.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now