確認刪除會發生錯誤

edited 十月 2013 in PHP新手區
請問一下我要寫刪除確認的功能,現在會跑出視窗但點確認沒動作,請問我怎麼改?
//刪除確認
function delete_data(p, sn){
var sure = window.confirm('確定要刪除此資料?');
if (!sure) return;
location.href="{$_SERVER}?op=del&dr="+dr+"&p="+p;
}

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4308

評論

  • edited 七月 2009
    也許你需要確認,在實際產生的網頁原始碼中,輸出的結果是否如同預期

    也建議安裝一個可以檢視 javascript 錯誤的工具,像是 Firefox + Firebug
  • edited 七月 2009
    錯了...
    location.href="{$_SERVER}?op=del&dr="+dr+"&p="+p;
    在javascript中要加入php變數要這樣做
    location.href="<?php echo {$_SERVER} ?>?op=del&dr="+dr+"&p="+p;
  • 280280
    edited 七月 2009
    一方面如 SoltyRain 大所說,網址輸出錯誤
    另一方面,這寫法有點繞口
    我是都這樣寫,給你參考

    function delete_data(p, sn){
    if (window.confirm('確定要刪除此資料?'))
    location.href="<?=$_SERVER?>?op=del&dr="+dr+"&p="+p;
    }
  • edited 七月 2009
    不好意思!目前我刪除還是不行呢,還有編輯的功能也有問題,感覺被我愈改愈亂,我附上完整的code請幫我看一下刪除及編輯為何不行??

    <?php
    //工具列
    function toolbar(){
    if(check_user($_SESSION["id"],$_SESSION["passwd"])){
    $main="
    <div class='toolbar'>
    <a href='{$_SERVER}'>報備一覽</a>
    <a href='{$_SERVER}?op=add_form'>新增報備</a>
    <a href='{$_SERVER}?op=search_form'>搜尋</a>
    <a href='{$_SERVER}?op=logout'>登出</a>
    </div>";
    }else{
    $main="
    <div class='toolbar'>
    <a href='{$_SERVER}?op=login_form'>登入</a>
    </div>";
    }
    return $main;
    }

    //註冊表單
    function register_form($the_id=""){
    if(!empty($the_id)){
    $op="modify_profile";
    $readonly="readonly";
    $mem=get_mem_data($the_id);
    }else{
    $op="register";
    $readonly="";
    }
    $main=<<<FORM
    <form action="{$_SERVER}" method="post">
    <table class="input_table">
    <tr>
    <td class="col_title">您的姓名:</td>
    <td class="col"><input type="text" name="reg[name]" value="{$mem}" class="txt"></td>
    </tr>
    <tr>
    <td class="col_title">電子郵件:</td>
    <td class="col"><input type="text" name="reg[email]" value="{$mem}" class="txt"></td>
    </tr>
    <tr>
    <td class="col_title">設定帳號:</td>
    <td class="col"><input type="text" name="reg[id]" value="{$mem}" class="txt" $readonly></td>
    </tr>
    <tr>
    <td class="col_title">設定密碼:</td>
    <td class="col"><input type="password" name="reg[passwd]" class="txt"></td>
    </tr>
    <tr>
    <td class="col_title">確認密碼:</td>
    <td class="col"><input type="password" name="reg[passwd2]" class="txt"></td>
    </tr>
    <td colspan="2" align="center">
    <input type="hidden" name="op" value="{$op}">
    <input type="submit" value="註冊" class="input_btn">
    </td>
    </tr>
    </table>
    </form>
    FORM;

    return $main;
    }


    //登入表單
    function login_form(){
    $main=<<<FORM
    <form action="{$_SERVER}" method="post">
    <table class="input_table">
    <tr>
    <td class="col_title">帳號:</td>
    <td class="col"><input type="text" name="id" class="txt"></td>
    </tr>
    <tr>
    <td class="col_title">密碼:</td>
    <td class="col"><input type="password" name="passwd" class="txt"></td>
    </tr>
    <td colspan="2" align="center">
    <input type="hidden" name="op" value="login">
    <input type="submit" value="登入" class="input_btn">
    </td>
    </tr>
    </table>
    </form>
    FORM;

    return $main;
    }

    //取得某人的資料
    function get_mem_data($the_id="") {
    global $link;
    if(empty($the_id))return;
    $sql="select * from id_passwd where id='{$the_id}'";
    $result=mysql_db_query("myweb",$sql,$link) or die("無法取得{$the_id}的資料!<br>".$sql);
    $data = mysql_fetch_assoc($result);
    return $data;
    }

    //身份確認
    function check_user($id="",$passwd="",$md5=false){
    if(empty($id) or empty($passwd))return false ;
    if($md5)$passwd=md5($passwd);
    $user=get_mem_data($id);
    if($user==$id and $user==$passwd){
    if(empty($_SESSION["id"])){
    $_SESSION["id"]=$id;
    $_SESSION["passwd"]=$passwd;
    $_SESSION["usn"]=$user;
    }
    return true;
    }
    return false;
    }

    //登出
    function logout(){
    $_SESSION = array();
    session_destroy();
    }

    //用來新增報備的表單
    function add_form($sn=""){
    if(!empty($sn)){
    $val=get_data($sn);
    $op="modify";
    $hidden_col="
    <input type='hidden' name='sn' value='{$sn}'>
    <input type='hidden' name='p' value='{$_GET}'>";
    }else{
    $op="add";
    }
    $main=<<<FORM
    <form action="{$_SERVER}" method="post" enctype="multipart/form-data">
    <table class="input_table">
    <tr>
    <td colspan="4" class="col_head_title">編輯報備資料</td>
    </tr>
    <tr>
    <td class="col_title">報備經銷商:</td><td class="col"><input type="text" name="data[dr]" value="{$val}" class="txt"></td>
    <td class="col_title">使用單位:</td><td class="col"><input type="text" name="data[units]" value="{$val}" class="txt"></td>
    </tr>
    <tr>
    <td class="col_title">聯絡人:</td><td class="col"><input type="text" name="data[cp]" value="{$val}" class="txt"></td>
    <td class="col_title">電話:</td><td class="col"><input type="text" name="data[phone]" value="{$val}" class="txt"></td>
    </tr>
    <tr>
    <td class="col_title">數量:</td><td class="col"><input type="text" name="data[amount]" value="{$val}" class="txt"></td>
    <td class="col_title">報備人:</td><td class="col"><input type="text" name="data[user]" value="{$val}" class="txt"></td>
    </tr>
    <tr>
    <td class="col_title">報備日期:</td><td class="col"><input type="text" name="data[rep_time]" value="{$val}" class="txt"></td>
    <td class="col_title">預計完成時間:</td><td colspan="3"><input type="text" name="data[ex_time]" value="{$val}" class="txt"></td>
    </tr>
    <tr>
    <td align="center">
    <input type="hidden" name="op" value="{$op}">
    $hidden_col
    <input type="submit" value="儲存" class="input_btn">
    </td>
    </tr>
    </table>
    </form>
    FORM;

    return $main;
    }

    //新增一筆通訊資料
    function add($data_array=array()){
    global $link;

    foreach($data_array as $col=>$val){
    if(!get_magic_quotes_gpc()){
    $val=addslashes($val);
    }
    ${$col}=$val;
    }

    $sql="insert into data_pag (dr,units,cp,phone,amount,rep_time,ex_time,user) values('{$dr} ' , '{$units}' , '{$cp}' , '{$phone}' , '{$amount}', '{$rep_time}' , '{$ex_time} ','{$user}')";
    mysql_db_query("hm_data",$sql,$link) or die("通訊錄資料無法寫入喔!<br>".$sql);
    $new_sn=mysql_insert_id();

    }

    //建立目錄
    function mk_dir($dir=""){
    //若無目錄名稱秀出警告訊息
    if(empty($dir))die("無目錄名稱");
    //若目錄不存在的話建立目錄
    if (!is_dir($dir)) {
    umask(000);
    //若建立失敗秀出警告訊息
    if(!mkdir($dir, 0777))die("無法建立目錄");
    }
    }

    //秀出資料
    function listall($key=""){
    global $link;

    //處理分頁
    $p=(empty($_GET))?1:$_GET;
    $num=7;
    $total=0;
    $start=($p-1)*$num;

    $limit=(empty($key))?"limit $start,$num":"";
    $sql="select * from data_pag $limit";
    $result=mysql_db_query("hm_data",$sql,$link) or die("無法取得資料!<br>".$sql);
    $addr_list="";
    while($db_data = mysql_fetch_row($result)){
    if(!empty($key)){
    $db_data_str=stripslashes(implode(",",$db_data));
    if(!ereg($key,$db_data_str))continue;
    $total++;
    }

    list($dr,$units,$cp,$phone,$amount,$rep_time,$ex_time,$user)=$db_data;

    if($_SESSION["id"]==aska or $_SESSION["usn"]==$usn){
    $admin_tool="
    <a href='{$_SERVER}?op=edit&p={$p}&sn={$dr}'>編輯</a><br>
    <a href='javascript:delete_data($p,$dr)'>刪除</a>";
    }else{
    $admin_tool="";
    }

    $addr_list.="<tr class='view'>
    <td rowspan='2' class='func'>
    $admin_tool
    </td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$dr</td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$units</td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$cp</td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$phone</td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$amount</td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$rep_time</td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$ex_time</td>
    <td rowspan='2' style='font-size:16px' nowrap align='center'>$user</td>
    </tr>
    <tr>
    <td colspan='3'>$addr</td>
    </tr>";
    }

    if(empty($key)){
    //取得總資料數
    $sql="select count(*) from data_pag";
    $result=mysql_db_query("hm_data",$sql,$link) or die("無法取得總資料數!<br>".$sql);
    list($total) = mysql_fetch_row($result);
    }
    $n=ceil($total/$num);
    $page_list="<select onChange=\"if(this.value!='') location.href = '{$_SERVER}?p=' + this.value\">";
    for($a=1;$a<=$n;$a++){
    $selected=($p==$a)?"selected":"";
    $page_list.="<option value='{$a}' $selected>第 $a 頁</option>";
    }
    $page_list.="</select>";

    $next_page=$p+1;
    $previous_page=$p-1;

    $nav="
    <div class='nav'>
    (全部共 $total 筆資料)
    <a href='{$_SERVER}?p={$previous_page}'>上一頁</a>
    $page_list
    <a href='{$_SERVER}?p={$next_page}'>下一頁</a>
    </div>";

    $main=<<<LIST_ALL
    <script language="JavaScript" type="text/JavaScript">
    <!--
    //刪除確認
    function delete_data(p, sn){
    var sure = window.confirm('確定要刪除此資料?');
    if (!sure) return;
    location.href="{$_SERVER}?op=del&sn="+sn+"&p="+p;
    }
    //-->
    </script>
    $nav
    <table class="list">
    <tr align="center">
    <th nowrap>功能</th>
    <th>編號</th>
    <th>報備經銷商</th>
    <th>使用單位</th>
    <th>聯絡人</th>
    <th>聯絡電話</th>
    <th>數量</th>
    <th>報備時間</th>
    <th>預計完成時間</th>
    <th>報備人</th>
    </tr>
    $addr_list
    </table>
    $nav
    LIST_ALL;

    return $main;
    }

    //取得某一筆資料
    function get_data($the_sn=""){
    global $link;
    if(empty($the_sn))return;
    $sql="select * from data_pag where sn='{$sn}'";
    $result=mysql_db_query("hm_data",$sql,$link) or die("無法取得{$sn}的資料!<br>".$sql);
    $data = mysql_fetch_assoc($result);
    return $data;
    }

    //變更某筆資料
    function modify($the_sn=""){
    global $link;
    if(empty($the_sn))return;

    $sql="update data_pag set dr='{$_POST} ' , units='{$_POST}' , cp='{$_POST}' , phone='{$_POST}' , amount='{$_POST}' , rep_time='{$_POST}' , ex_time='{$_POST}' ,user='{$_POST} where sn='{$the_sn}'";
    mysql_db_query("hm_data",$sql,$link) or die("無法修改通訊錄資料喔!<br>".$sql);
    }


    //搜尋表單
    function search_form(){
    $main=<<<FORM
    <form action="{$_SERVER}" method="post">
    <table class="input_table">
    <tr>
    <td class="col_title">請輸入關鍵字:</td>
    <td class="col"><input type="text" name="key" class="txt"></td>
    <td>
    <input type="hidden" name="op" value="search">
    <input type="submit" value="搜尋" class="input_btn">
    </td>
    </tr>
    </table>
    </form>
    FORM;

    return $main;
    }



    ?>
    [/email]
  • edited 七月 2009
    1.沒有看到關於刪的描述
    delete from .....
    2.javascript不能存取sql
    3.$_POST<--你確定post進來是這樣?
    猜測應該是$_POST
Sign In or Register to comment.