我是Ajax的新手,我正在尝试使用相同的CRUD操作。我有一个 index.php
页面,我可以插入和显示数据,我也有链接来更新和删除数据.Create,Read,Update工作正常。现在的问题是,当我点击删除链接时,我已成功将ID发送到 delete_back.php
,并且该值已被删除但成功消息应该打印在< strong> index.php
因为我在那边有一个名为 #delete_msg
的div,但不知何故成功消息正在 delete_back.php
上打印
<div id="delete_msg"></div>
<?php
include("config.php");
$data=mysqli_query($con,"select * from student");
$col=mysqli_num_fields($data);
echo "<table>";
while($row=mysqli_fetch_array($data))
{
echo "<tr>";
for($i=0;$i<$col;$i++)
{
echo "<td>".$row[$i]."</td>";
}
echo "<td><a href='update_front.php?id=$row[0]&nm=$row[1]&add=$row[2]&cont=$row[3]'> Update </a></td>";
echo "<td><a class='delete' href='delete_back.php?id=$row[0]'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$id=$_GET['id'];
include("config.php");
$data=mysqli_query($con,"delete from student where id='$id'");
if($data=="true")
{
echo "Data Deleted";
}
else
{
echo "Delete Error";
}
?>
$(".delete").click(function(event){
event.preventDefault();
$.ajax({
url:"delete_back.php",
dataType:"html",
success:function(msgStr){
$("#delete_msg").html(msgStr);
}
})
})
答案 0 :(得分:5)
将data-id属性添加到锚标记:
mystr = 'student1 90\nstudent2 85\nstudent3 98'
[[i[0], int(i[1])] for i in (k.split() for k in mystr.split('\n'))]
# [['student1', 90], ['student2', 85], ['student3', 98]]
你可以在你的ajax中做到:
echo "<td><a id='delete' data-id='".$row[0]."' href='#'>Delete</a></td>";
在你的php文件中:
$("#delete").click(function(event){
var obj = $(this); // first store $(this) in obj
var id = $(this).data('id'); // get id of data using this
$.ajax({
url:"delete_back.php",
dataType:"html",
data: { id: id },
type: "Post",
success:function(msgStr){
$("#delete_msg").html(msgStr);
}
})
})
答案 1 :(得分:3)
首先,我真的希望你没有一个打印多个具有相同ID的锚点的循环......
也就是说,根据你的HTML,你的JS应该像跟随,否则你将失去你的id
值:
$("#delete").click(function(event){
event.preventDefault();
var my_href = $(this).attr('href');
$.ajax({
url: my_href,
dataType:"html",
success:function(msgStr){
$("#delete_msg").html(msgStr);
}
});
return false;
});
但这不是一个非常好的编码风格..我建议使用html5 data-
属性来存储您的id
值
echo "<td><a id='delete' data-id='".$row[0]."' href='#'>Delete</a></td>";
如果您不想更改网址,也可以使用javascript:;
代替#
。
你的JS应该像:
$("#delete").click(function(event){
// you can avoid preventing default behaviour because your anchor doesn't have any href value
var del_id= $(this).data('id');
$.ajax({
url: "delete_back.php",
dataType:"html",
data: {id: del_id} // use object for better reading
success:function(msgStr){
$("#delete_msg").html(msgStr);
}
});
});
我还建议您查看.done()
构建LINK和deferred.done()
答案 2 :(得分:2)
我认为删除链接来自包含其他删除链接的循环。如果是这样,这就是你应该做的。
echo '<td><a class="lnk-delete" href="#" data-id="'.$row[0].'">Delete</a></td>'; //The delete links should have 1 class name
然后进行ajax调用
$('.lnk-delete').click(function(e){
var id = $(this).data('id');
ajax({
url: 'delete_back.php'
data: {id: id},
type: 'POST',
success: function(data){
$('#delete_msg').html(data);
}
});
});
获取要在php中删除的项目的ID
$id = $_POST['id'];
答案 3 :(得分:1)
with from_to_table as (
SELECT tid, valid_from, LEAD(valid_from, 1, null) OVER (ORDER BY
valid_from)-1 AS valid_to,fee
FROM fee_table
)
select fee from from_to_table
where to_date(:mydate,'dd/mm/rrrr') between valid_from and nvl(valid_to,to_date(:mydate,'dd/mm/rrrr')+1)