Php jqueryajax删除记录
我试图删除表中逗号分隔字符串中的值。如果我手动设置post值,那么执行此操作的脚本(delete_url.php)本身就可以正常工作,但是我在使用ajax将这些值传递给脚本时遇到了问题。在我要传递的值的页面中,我有:Php jqueryajax删除记录,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我试图删除表中逗号分隔字符串中的值。如果我手动设置post值,那么执行此操作的脚本(delete_url.php)本身就可以正常工作,但是我在使用ajax将这些值传递给脚本时遇到了问题。在我要传递的值的页面中,我有: <script type="text/javascript"> $(document).ready(function() { $('.delete').click(function() { $.
<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>
$(文档).ready(函数(){
$('.delete')。单击(函数(){
$.ajax({
键入:“POST”,
url:“../scripts/delete_link.php”,
data:'link='+$(this.attr('data_link')+'&topic_pk='+$(this.attr('data_topic')),
成功:函数(){
}
});
});
});
以及:
我可能应该使用json,但不确定正确的方法。感谢您的帮助。我已使用以下内容更新了代码,现在正在删除记录并删除页面上的条目:
<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>
$(文档).ready(函数(){
$('table#delTable td a.delete_link')。单击(函数()
{
if(确认(“是否确实要删除此行?”)
{
var id=$(this.parent().parent().attr('id');
变量数据='id='+id;
var parent=$(this.parent().parent();
$.ajax(
{
类型:“POST”,
url:“../scripts/delete_link.php”,
数据:'link='+$(this).attr('data_link')+'、topic_pk='+$(this).attr('data_topic')+'、topic_introduction='+$(this).attr('data_introduction'),
cache:false,
成功:函数()
{
fadeOut('fast',function(){$(this.remove();});
}
});
}
});
});
下表:
<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>
并删除_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();
?>
如果您在一个数据库列中存储多个值,那么您就做错了!抱歉,这不是问题所在,我只能这样做。我刚刚看到错误,应该是$('.delete_link')。单击(function(){
<?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();
?>