如果用户确定要从数据库中删除行,我将使用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不起作用,有人能提供快速建议吗?
您应该更改您的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;
}
?>
onclick="return confirm('您确定要删除此项目吗?');"
我想知道你为什么要检查这个:
var x =document.getElementById("brisi").value;
你不用它。
我还希望"brisi"是这个元素唯一的ID。
接下来,您将使用ajax进行POST请求:
$.ajax({ url: 'edit/delete.php?a=1',
type: 'post',
success: function(output) {
document.getElementById("brisi").value = output;
}
});
我注意到在你的PHP中,你正试图获得你发布的vrable:
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表单。