使用 ajax 删除点击记录
delete a record on click using ajax
我想通过将 while 循环中生成的动态值传递给 ajax 来删除记录。
我想在 jquery 中传递 $ef 值。
(即)在删除图像的 onclick 事件中删除示例 hrms trail.doc 文件。
现在图像标签的 onclick 事件不显示任何动作
while($fet=mysql_fetch_assoc($sql1))
{
$file=$fet['f_name'];
$ef=$fet['ef_id'];
$next1 = basename($file);
echo "<h3><a class=doc href='".$file."' title='".$file."' download><p style='margin-left:1cm;'>".$next1."</a>";
echo '<img src="image/delete1.png" id=".$ef."width="10" height="10" title="Remove" onclick="javascript:myFunction();">';
}
我的jqueryajax代码
<script>
function myFunction()
{
var rmvfile=$("doc").val();
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{rmvfile: rmvfile},
success:function(msg){
if (msg.length> 0) {
alert(msg);
} }
});
}
php delete_emp_file.php
中的代码
$s=$_POST['rmvfile'];
include "config.php";
$echeck="delete from employee_file where ef_id='".$_POST['rmvfile']."'";
$echk=mysql_query($echeck);
$ecount=mysql_num_rows($echk);
if($ecount>='1')
{
echo "file deleted";
}
试试这个,
while($fet=mysql_fetch_assoc($sql1))
{
$file=$fet['f_name'];
$ef=$fet['ef_id'];
$next1 = basename($file);
echo "<h3><a class='doc' href='".$file."' title='".$file."'><p style='margin-left:1cm;'>".$next1."</a>";
echo '<img src="image/delete1.png" id=".$ef." width="10" height="10" title="Remove" onclick="javascript:myFunction();">';
}
还有你的AJAX:
function myFunction()
{
var rmvfile=$(".doc").val();
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{rmvfile: rmvfile},
success:function(msg){
if (msg.length> 0) {
alert(msg);
}
}
});
}
在你的 delete_emp_file.php,
$echeck="delete from employee_file where ef_id='".$_POST['rmvfile']."'";
您可以传递 $s
而不是 $_POST['rmvfile']
$echeck="delete from employee_file where ef_id='$s'";
您想传递 $ef 变量:
while($fet=mysql_fetch_assoc($sql1))
{
$file=$fet['f_name'];
$ef=$fet['ef_id'];
$next1 = basename($file);
echo "<h3><a class='doc' id=".$ef." href='".$file."' title='".$file."'><p style='margin-left:1cm;'>".$next1."</a>";
echo '<img src="image/delete1.png" width="10" height="10" title="Remove" onclick="javascript:myFunction();">';
}
您的 Ajax
代码
function RemoveFile(id)
{
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{rmvfile: id},
success:function(msg){
if (msg.length> 0) {
alert(msg);
}
}
});
}
$(".doc").on('touchstart click',function() {
var file_id = $(this).prop('id');
RemoveFile(file_id);
});
如果您想同时传递 href 和 id,请执行以下操作:
function RemoveFile(id,link)
{
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{fid: id, rmvfile: link},
success:function(msg){
if (msg.length> 0) {
alert(msg);
}
}
});
}
$(".doc").on('touchstart click',function() {
var file_id = $(this).prop('id');
var file_link = $(this).prop('href');
RemoveFile(file_id,file_link);
});
这是我的删除图像的代码,如您所述
<ul class="hoverbox">
<?php
for($k=0;$k<$cardslen;$k++)
{
echo '<li class="inlineclass" id="ss'.$imagenames[$k]->id.'">';
echo '<div class="scrolllist"><div class="img-wrap" id="s'.$imagenames[$k]->id.'"><span class="close" id="'.$imagenames[$k]->id.'">×</span>';
echo '<img id="'.$imagenames[$k]->id.'" data-id="'.$imagenames[$k]->id.'~'.$folder.'" alt="'.$imagenames[$k]->title.'" class="imglarge" id="imgSmile<?php echo $k;?>" style="height: 120px;" src="../images/morfeoshow/'.$folder.'/thumbs/'.$imagenames[$k]->filename.'">';
echo '</div>';
echo '</li>';
}
?>
</ul>
这是脚本代码
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$('.img-wrap .close').on('click', function() {
var id = $(this).closest('.img-wrap').find('.imglarge').data('id');
//alert(id);
var r = confirm("Do you want to remove this image!");
if (r == true) {
$.ajax({
url : "yoururl",
type: "POST",
data :{imageid:id},
success: function(data)
{
//alert(data);
$('#ss'+data).hide();
$('#sss'+data).hide();
$('#'+data).hide();
$('#s'+data).hide();
//location.reload();
},});
}
});
});//]]>
</script>
在 ajax 文件上,我只有代码删除 mysql 查询删除记录和从文件夹中删除图像的代码。
在此代码中 ajax 将删除图像,并且在 ajax 调用成功后隐藏删除图像并且页面不会刷新。
我认为这段代码会对你有所帮助
我通过以下代码解决了这个问题
while($fet=mysql_fetch_assoc($sql1))
{
$next=$fet['f_name'];
$next1 = basename($next);
$ef=$fet['f_id'];
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download><p style="margin-left:1cm;">'.$next1.'</a>';
echo '<img src="image/delete1.png" alt="delete" style="width:10px;height:10px" title="Remove" onclick="myFunction('.$fet['f_id'].');">';
}
function myFunction(fid)
{
var rmvfile=fid;
if (confirm("Are you sure you want to Delete the file?") == true) {
if(fid!='')
{
$.ajax({
type:'post',
url:'delete_adm_file.php',
data:{rmvfile: rmvfile},
success:function(msg){
location.reload();
}
});
} } }
我想通过将 while 循环中生成的动态值传递给 ajax 来删除记录。 我想在 jquery 中传递 $ef 值。 (即)在删除图像的 onclick 事件中删除示例 hrms trail.doc 文件。 现在图像标签的 onclick 事件不显示任何动作
while($fet=mysql_fetch_assoc($sql1))
{
$file=$fet['f_name'];
$ef=$fet['ef_id'];
$next1 = basename($file);
echo "<h3><a class=doc href='".$file."' title='".$file."' download><p style='margin-left:1cm;'>".$next1."</a>";
echo '<img src="image/delete1.png" id=".$ef."width="10" height="10" title="Remove" onclick="javascript:myFunction();">';
}
我的jqueryajax代码
<script>
function myFunction()
{
var rmvfile=$("doc").val();
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{rmvfile: rmvfile},
success:function(msg){
if (msg.length> 0) {
alert(msg);
} }
});
}
php delete_emp_file.php
中的代码 $s=$_POST['rmvfile'];
include "config.php";
$echeck="delete from employee_file where ef_id='".$_POST['rmvfile']."'";
$echk=mysql_query($echeck);
$ecount=mysql_num_rows($echk);
if($ecount>='1')
{
echo "file deleted";
}
试试这个,
while($fet=mysql_fetch_assoc($sql1))
{
$file=$fet['f_name'];
$ef=$fet['ef_id'];
$next1 = basename($file);
echo "<h3><a class='doc' href='".$file."' title='".$file."'><p style='margin-left:1cm;'>".$next1."</a>";
echo '<img src="image/delete1.png" id=".$ef." width="10" height="10" title="Remove" onclick="javascript:myFunction();">';
}
还有你的AJAX:
function myFunction()
{
var rmvfile=$(".doc").val();
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{rmvfile: rmvfile},
success:function(msg){
if (msg.length> 0) {
alert(msg);
}
}
});
}
在你的 delete_emp_file.php,
$echeck="delete from employee_file where ef_id='".$_POST['rmvfile']."'";
您可以传递 $s
而不是 $_POST['rmvfile']
$echeck="delete from employee_file where ef_id='$s'";
您想传递 $ef 变量:
while($fet=mysql_fetch_assoc($sql1))
{
$file=$fet['f_name'];
$ef=$fet['ef_id'];
$next1 = basename($file);
echo "<h3><a class='doc' id=".$ef." href='".$file."' title='".$file."'><p style='margin-left:1cm;'>".$next1."</a>";
echo '<img src="image/delete1.png" width="10" height="10" title="Remove" onclick="javascript:myFunction();">';
}
您的 Ajax
代码function RemoveFile(id)
{
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{rmvfile: id},
success:function(msg){
if (msg.length> 0) {
alert(msg);
}
}
});
}
$(".doc").on('touchstart click',function() {
var file_id = $(this).prop('id');
RemoveFile(file_id);
});
如果您想同时传递 href 和 id,请执行以下操作:
function RemoveFile(id,link)
{
$.ajax({
type:'post',
url:'hrms/delete_emp_file.php',
data:{fid: id, rmvfile: link},
success:function(msg){
if (msg.length> 0) {
alert(msg);
}
}
});
}
$(".doc").on('touchstart click',function() {
var file_id = $(this).prop('id');
var file_link = $(this).prop('href');
RemoveFile(file_id,file_link);
});
这是我的删除图像的代码,如您所述
<ul class="hoverbox">
<?php
for($k=0;$k<$cardslen;$k++)
{
echo '<li class="inlineclass" id="ss'.$imagenames[$k]->id.'">';
echo '<div class="scrolllist"><div class="img-wrap" id="s'.$imagenames[$k]->id.'"><span class="close" id="'.$imagenames[$k]->id.'">×</span>';
echo '<img id="'.$imagenames[$k]->id.'" data-id="'.$imagenames[$k]->id.'~'.$folder.'" alt="'.$imagenames[$k]->title.'" class="imglarge" id="imgSmile<?php echo $k;?>" style="height: 120px;" src="../images/morfeoshow/'.$folder.'/thumbs/'.$imagenames[$k]->filename.'">';
echo '</div>';
echo '</li>';
}
?>
</ul>
这是脚本代码
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$('.img-wrap .close').on('click', function() {
var id = $(this).closest('.img-wrap').find('.imglarge').data('id');
//alert(id);
var r = confirm("Do you want to remove this image!");
if (r == true) {
$.ajax({
url : "yoururl",
type: "POST",
data :{imageid:id},
success: function(data)
{
//alert(data);
$('#ss'+data).hide();
$('#sss'+data).hide();
$('#'+data).hide();
$('#s'+data).hide();
//location.reload();
},});
}
});
});//]]>
</script>
在 ajax 文件上,我只有代码删除 mysql 查询删除记录和从文件夹中删除图像的代码。 在此代码中 ajax 将删除图像,并且在 ajax 调用成功后隐藏删除图像并且页面不会刷新。 我认为这段代码会对你有所帮助
我通过以下代码解决了这个问题
while($fet=mysql_fetch_assoc($sql1))
{
$next=$fet['f_name'];
$next1 = basename($next);
$ef=$fet['f_id'];
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download><p style="margin-left:1cm;">'.$next1.'</a>';
echo '<img src="image/delete1.png" alt="delete" style="width:10px;height:10px" title="Remove" onclick="myFunction('.$fet['f_id'].');">';
}
function myFunction(fid)
{
var rmvfile=fid;
if (confirm("Are you sure you want to Delete the file?") == true) {
if(fid!='')
{
$.ajax({
type:'post',
url:'delete_adm_file.php',
data:{rmvfile: rmvfile},
success:function(msg){
location.reload();
}
});
} } }