2013-02-18 77 views
2

我想刪除表中逗號分隔字符串中的值。這樣做的腳本(delete_url.php)如果我手動設置帖子值,它自己可以正常工作,但我遇到了一個問題,通過ajax將這些值傳遞給腳本。在此我想從我值傳遞的頁面有:jquery ajax刪除記錄

<script type="text/javascript"> 
      $(document).ready(function() { 

       $('.delete').click(function() { 
     $.ajax({ 
      type: 'POST', 
      url: '../scripts/delete_link.php', 
      data: 'link=' + $(this).attr('data_link') + '&topic_pk=' + $(this).attr('data_topic'), 
      success: function() { 

      } 
     });   
    }); 
      }); 
     </script> 

和:

<a class='delete_link' href='#' data_link='<?php echo urlencode($link); ?>' data_topic='<?php echo $topic_pk; ?>' onclick="return confirm('Are you certain you want to DELETE this link?')";><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a> 

我大概應該是使用JSON,但不知道正確的方式來做到這一點。謝謝你的幫助。

+1

如果您在一個數據庫列中存儲多個值,**您做錯了!** – 2013-02-18 02:15:59

+1

對不起,但那不是問題,也是我能做到這一點的唯一方法。 – IlludiumPu36 2013-02-18 02:17:53

+0

我剛纔看到錯誤,應該是$('。delete_link')。click(function(){ – IlludiumPu36 2013-02-18 02:18:30

回答

1

我已經更新了我的代碼如下因素和現在的作品刪除記錄和刪除頁面上的條目:

<script type="text/javascript"> 
$(document).ready(function(){ 

    $('table#delTable td a.delete_link').click(function() 
    { 
     if (confirm("Are you sure you want to delete this row?")) 
     { 
      var id = $(this).parent().parent().attr('id'); 
      var data = 'id=' + id ; 
      var parent = $(this).parent().parent(); 

      $.ajax(
      { 
        type: "POST", 
        url: '../scripts/delete_link.php', 
        data: 'link=' + $(this).attr('data_link') + '&topic_pk=' + $(this).attr('data_topic') + '&topic_introduction=' + $(this).attr('data_introduction'), 
        cache: false, 

        success: function() 
        { 
        parent.fadeOut('fast', function() {$(this).remove();}); 
        } 
      }); 
     } 
    }); 
}); 

</script> 

而且表:

<table id='delTable' width="100%" border="0" cellpadding="5"> 
<?php 
if(!empty($retrieved_links)){ 
    foreach($retrieved_links as $link){ 
?> 
    <tr> 
    <td><?php echo $link; ?></td> 
    <td width='16' align='center' valign='middle'><a class='delete_link' href='#' data_link='<?php echo urlencode($link); ?>' data_topic='<?php echo $topic_pk; ?>' data_introduction='<?php echo $topic_introduction; ?>'><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a></td> 
    </tr> 
<?php } 
} 
?> 
</table> 

而且delete_link.php:

<?php 
require_once('../connection/connect.php'); 
mysql_select_db($database, $connection); 

$link = urldecode($_POST['link']); 
$topic_pk = $_POST['topic_pk']; 
$topic_introduction = $_POST['topic_introduction']; 


if(!empty($link)){ 

$query_get_topic = "SELECT * FROM topic WHERE topic_pk = '$topic_pk'"; 
$result_get_topic = mysql_query($query_get_topic, $connection) or die(mysql_error()); 
$row_get_topic = mysql_fetch_assoc($result_get_topic); 

$retrieved_links = explode(",", $row_get_topic['links']); 

$delete_link = array_search($link,$retrieved_links); 

unset($retrieved_links[$delete_link]); 
$updated_links = mysql_real_escape_string(implode(',',$retrieved_links)); 

$links_body = str_replace(',', '<p>', $updated_links); 
$topic = $topic_introduction . '<p>' . $links_body; 

$query = "UPDATE topic SET links = '$updated_links', topic = '$topic' WHERE topic_pk = '$topic_pk'"; 
$result = mysql_query($query, $connection) or die(mysql_error()); 
} 
mysql_close(); 
?>