用js confirm + php删除链接

时间:2011-07-06 17:21:06

标签: php javascript html

这是删除链接

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

显然,当点击该链接时,门户网站会自动删除数据,我认为这是一个ajax,因为页面不会刷新。所以我被要求添加一个确认弹出窗口,要求用户单击是或否,如果他想要删除数据,并在确认框中,它应该提到要删除的数据的名称,例如

"are you sure you want to delete row_title ?"

这是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);
                             }


                           }
                         });

       }

它很长,我认为如果要求只是在确认弹出框中点击“是”时删除数据,我就不需要所有这些东西

现在这里是删除PHP函数

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;

1 个答案:

答案 0 :(得分:1)

我认为您不想开始更改上面的代码,因为它用于通过ajax将相关数据传递给php脚本。

您需要的是javascript提示拦截链接点击并为用户提供继续或取消删除操作的选项。这是对的吗?

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

在“deleteThisArtWork”javascript函数开始时,您需要显示提示。

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;
    }
}

这应该可以阻止用户意外删除记录,而不必至少不小心点击确认弹出窗口!

如果要使确认弹出窗口中的文本动态化,则需要将动态文本作为变量传递给“deleteThisArtwork”方法,或者使用一些javascript从页面上的其他元素中绘制它。