如果他确定要从数据库中删除行,我正在使用javascript来提醒用户。 在html表中,我使用列来删除行。
<td><center><a href="" id="brisi" value="<?=$r['Id']; ?>" class="delete" data-confirm="Are you sure that you want to delete?"><i class="fa fa-times"></i></center>
我正在使用脚本提醒用户然后导航到php脚本以删除行
<script>
var deleteLinks = document.querySelectorAll('.delete');
for (var i = 0; i < deleteLinks.length; i++) {
deleteLinks[i].addEventListener('click', function(event) {
event.preventDefault();
var choice = confirm(this.getAttribute('data-confirm'));
if (choice==true) {
var x =document.getElementById("brisi").value;
$.ajax({ url: 'edit/delete.php?a=1',
type: 'post',
success: function(output) {
document.getElementById("brisi").value = output;
}
});
}
if (choice==false) {
return false;
}
});
}
</script>
Php脚本
<?php
if ($_GET['a']==1) {
$id = test_input($_POST['brisi']);
echo $id;
}
?>
我正在尝试发送每行的值。出于某种原因,我的js不起作用,有人可以提供快速建议吗?
答案 0 :(得分:0)
你应该改变你的HTML
<td><center><a href="javascript:;" data-value="<?=$r['Id']; ?>" class="delete" data-confirm="Are you sure that you want to delete?"><i class="fa fa-times"></i></center>
并且还会更改javascript
$('.delete').off('click').on('click', function(e){
e.preventDefault();
var choice = confirm($(this).data('confirm'));
if(choice){
var x = $(this).data('value');
$.ajax({
url: 'edit/delete.php?a=' + x,
type: 'post',
success: function(output) {
document.getElementById("brisi").value = output;
}
});
}
else{
return false;
}
});
和php代码
<?php
if (isset($_GET['a']) && $_GET['a'] != ''){
$id = test_input($_GET['a']);
echo $id;
}
?>
答案 1 :(得分:0)
onclick =“返回确认('您确定要删除此项吗?');”
答案 2 :(得分:0)
我想知道你为什么要检查这个:
var x =document.getElementById("brisi").value;
你不能使用它。
我也希望&#39; brisi&#39;是仅此元素的唯一ID。
接下来,您正在使用ajax执行POST请求:
$.ajax({ url: 'edit/delete.php?a=1',
type: 'post',
success: function(output) {
document.getElementById("brisi").value = output;
}
});
我注意到你在PHP中试图获取你发布的vriable:
if ($_GET['a']==1) {
$id = test_input($_POST['brisi']);
echo $id;
}
你应该这样做:
if (isset($_POST['a') && $_POST['a'] == 1) {
$id = test_input($_POST['brisi']);
echo $id;
}
最后这个功能是做什么的:
test_input($_POST['brisi'])
您没有使用此索引发布任何值。
回顾如果您需要更多帮助,请发布完整的HTML表单。