Solved

return confirm and jquery bind

Posted on 2014-04-23
4
642 Views
Last Modified: 2014-04-30
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
Comment
Question by:Robert Saylor
  • 3
4 Comments
 
LVL 4

Expert Comment

by:Jim Riddles
ID: 40019811
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
 
LVL 6

Author Comment

by:Robert Saylor
ID: 40020030
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
 
LVL 6

Accepted Solution

by:
Robert Saylor earned 0 total points
ID: 40021755
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
 
LVL 6

Author Closing Comment

by:Robert Saylor
ID: 40031575
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now