开发者

delete link with js confirm + php

开发者 https://www.devze.com 2023-03-18 21:51 出处:网络
this was the delete link <divclass=\"artworkdelete\"> <a href=\"javascript:void(0);\" id=\"del_<?=$artworkDetails[\'artwork_id\'] ?>_<?=$temp_cat_id; ?>\" onclick=\"deleteThisAr

this was the delete link

    <div  class="artworkdelete">
   <a href="javascript:void(0);" id="del_<?=$artworkDetails['artwork_id'] ?>_<?=$temp_cat_id; ?>" onclick="deleteThisArtWork(this.id)">Delete</a>
 </div>

apparently, when that link is clicked, the portal deletes the data automatically, i think it's an ajax thing because the page doesn't refresh. so i was ask to add a confirm pop up to ask the user to click yes or no if he wants to delete the data or not, and within the confirm box , it should mention the name of the data to be deleted like e.g

"are you sure you want to delete row_title ?"

here's the function of the deleteThisArtWork()

       function deleteThisArtWork(artwork_id){
                        var artwork_id = artwork_id.split('_');
                        var cat_id=artwork_id[2];
                        artwork_id = artwork_id[1];
                        //$('#divStatus').html('processing request, please wait');
                        //$(".pleaseWait").dialog("open");
                        openLightBox();
                        $.ajax({
                            type: 'POST',
                            url: '<?php echo BASE_URL;?>ajax/ajax_methods_gallery.php',
                            data: 'deleteartwork=yes&artwork_id='+artwork_id+'&category_id='+cat_id,
                            success: function(msg){
                             //alert(msg);
                            msg = 'done';
                            var status=msg;
                            var deleted='';

                            if(status == 'done') {
                                 var temp_lid = 'li_'+artwork_id+'_'+cat_id+'_';
                                 //alert(counter);
                                 for(var v=1;v<counter;v++){
                                    var curid = tempIdArr[v];
                                    curid = curid.split('#');
                                    var curlid = curid[0];
                                    if(temp_lid == curlid){
                                        var del_aw_pos = curid[1];
                                        break;
                                    }
                                 }

                                 del_aw = temp_lid+'#'+del_aw_pos;
                                 var i = 1;
                                 var j =0;
                                 var op = false;
                                 var delpoint;
                                 var endpoint;
                                 var delcatid = '';
                                 var artcounter = 0;
                                 var artcounterArr = new Array();
                                 $(".sortli").each(function (){
                                    var atid = this.id.split('_');
                                    //if(atid[2]!=cat_id)return;
                                    if(this.id == del_aw){
                                        deleted = 'yes';
                                        delcatid = atid[2];
                                        $(this).remove();
                                        op = true;
                                        i=i+1;
                                        delpoint=i-1;
                                        //alert('D'+delpoint);
                                        //return;
                                    }
                                    if(atid[2]==cat_id){endpoint = i-1;artcounter=artcounter+1;}
                                    else if(j==0 && artcounter > 0){
                                        if(artcounter>0)artcounter = artcounter-1;
                                        //else artcounterArr[j] = 0;
                                        artcounterArr[j] = artcounter;
                                        j=j+1;
                                        artcounter = 0;
                                    }
                                    i = i + 1;
                                });
                                //alert(delpoint)
                                //alert(endpoint);
                                //alert(artcounterArr[0]);
                                 if(op){
                                      for(var k=delpoint; k<counter-1;k++){
                                           var orderVal1 = tempOrderArr[k];
                                           if(k<endpoint)document.getElementById('sortvalid_'+(k+1)).innerHTML = orderVal1;
                                           document.getElementById('sortvalid_'+(k+1)).id = 'sortvalid_'+(k);
                                           document.getElementById('sortdn_'+(k+1)).id = 'sortdn_'+(k);
                                           document.getElementById('sortup_'+(k+1)).id = 'sortup_'+(k);
                                           var t = tempIdArr[(k+1)].split('#');
                                           t=t[0];
                                           document.getElementById(tempIdArr[(k+1)]).id = t+'#'+k;
                                      }

                                     $(".rowHead").each(function (){
                                        var taid = this.id;
                        开发者_开发问答                var sp = this.id.split("^");
                                        var a1 = sp[1];
                                        //alert(a1);
                                        //alert(cat_id);
                                        if(parseInt(a1)>parseInt(cat_id)){
                                            var a2 = sp[2];
                                            var ta = 'lititle^'+a1+'^';
                                            //alert(ta);
                                            document.getElementById(taid).id = ta+(a2-1);
                                        }
                                     });

                                     var a2temp;
                                     var a1temp;
                                     var delcat=null;
                                     var rowHeadLast;

                                     $(".rowHead").each(function (){
                                        //var taid = this.id;
                                        rowHeadLast = this;
                                        var sp = this.id.split("^");
                                        var a1 = sp[1];
                                        var a2 = sp[2];
                                        if(a2temp == a2 && delcat==null){delcat = a2temp; delcatid=a1temp;}
                                        a2temp = a2;
                                        a1temp = a1;

                                     });
                                     var delok = false;
                                     $(".rowHead").each(function (){
                                        //var taid = this.id;
                                        //alert(deleted);
                                        var sp = this.id.split("^");
                                        var a1 = sp[1];
                                        var a2 = sp[2];
                                        if(delcat == a2 && a1==delcatid && deleted==''){delok= true;deleted='yes';$(this).remove();}

                                     });
                                     //alert(delcatid);
                                     //if(!artcounterArr[0])alert('d');
                                     if(!delok){
                                        $(".rowHead").each(function (){
                                            var sp = this.id.split("^");
                                            var cid_t = sp[1];
                                            if(!artcounterArr[0] && delcatid == cid_t)$(this).remove();
                                            //else if(artcounterArr[0]<=0)$(this).remove();
                                        });

                                     }
                                     if(deleted ==''){
                                        $(rowHeadLast).remove();
                                     }
                                 }
                                 setDivsInArray();
                                 //$(".pleaseWait").dialog("close");
                                 closeLightBox();
                             }
                             else if(status == 'DBDelete:error'){
                                 //$('#row_'+artwork_id).fadeOut(3500);
                                 $('#divStatus').fadeIn(500);
                                 $('#divStatus').html('<b>Artwork Delete Error</b>');
                                 $('#divStatus').fadeOut(4500);
                             }


                           }
                         });

       }

it's quite long , I think I don't need all of those stuff if the requirement is just delete the data when "Yes" was clicked from the confirm pop up box

now here's the delete PHP function

function deleteArtWork($artwork_id,$category_id){

    $artwork_cat_lookup_del = "delete from artwork_category_lookup where artwork_id = '$artwork_id' AND category_id='$category_id'";
    if(mysql_query($artwork_cat_lookup_del)){     
        $userObj = new User();
        $allArtWorkByCat = $userObj->allArtWorkByCat($category_id);
        for($itr = 0; $itr<count($allArtWorkByCat); $itr++){
            $ordr = $itr + 1;
            $art_id = $allArtWorkByCat[$itr]['artwork_id'];
            $updateSQL = "update artwork_category_lookup set artwork_display_order='$ordr'  where artwork_id = '$art_id' AND category_id = '$category_id'";
            mysql_query($updateSQL);
        }
        $action = $userObj->userActions('Artwork id: '.$artwork_id.' is deleted', 'Gallery');
        $userObj->setActionintoDB($action);
        echo 'done';
    }
    else echo 'DBDelete:error';
    return;


I don't think that you want to start changing that code above as that is used to pass the relevant data to the php script via ajax.

What you need is a javascript prompt to intercept the link click and give the user an option to continue or cancel the deletion action. Is this correct?

http://www.tizag.com/javascriptT/javascriptconfirm.php

At the start of the "deleteThisArtWork" javascript function you need to display a prompt.

function deleteThisArtWork(artwork_id){
    var answer = confirm("Are you sure you want to delete this record?");
    if (answer){
        //do the rest of the function as usual, i.e. delete row via ajax.
    }else{
        return false;
    }
}

That should stop the user from accidentally deleting a record without at least having to accidentally click on a confirmation popup as well!

If you want to make the text in the confirm popup dynamic, you would need to either pass in the dynamic text as a variable to the "deleteThisArtwork" method or draw it from another element on the page using some javascript.

0

精彩评论

暂无评论...
验证码 换一张
取 消