gpt4 book ai didi

javascript - 使用 PHP 和 Javascript 删除选定 ID 的选定行

转载 作者:行者123 更新时间:2023-11-29 19:27:08 25 4
gpt4 key购买 nike

我试图通过将参数传递到 URL 来删除选定 ID 的行。假设我有entryID 1和2,每当我尝试选择并删除entryID 1的内容时,它都会成功删除entryID 1的内容,但问题是当我选择删除entryID 2时,它仍然删除entryID 1而不是2 .我正在考虑变量var row = '".$rows['Blog_ID']."';的内容即使我选择其他方式,也不会更改并且仅保留 EntryID 1 的值。

这是我迄今为止尝试过的..

<?php
include("../Connection.php");
$post_query="Select * from indexview order by Blog_ID Desc";
$postsql=mysqli_query($connect_db,$post_query) or die('Connection unsuccessful');

while($rows=mysqli_fetch_array($postsql,MYSQL_ASSOC)){
echo "<div id='posts'>";
echo" <select onchange = 'down(this.value)' id='downpng' name='downpng'>
<option value='void'></option>
<option value = 'edit'>Edit Blog</option>
<option value ='delete'>Delete</option>
</select>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​";


echo
"<script>

function down(temp) {

var row = ".$rows['Blog_ID'].";
var id = '".$_GET['id']."';



if(temp=='delete'){

var con = confirm('Are you sure?');
if(con){
window.location = 'google.php?entryID=' + row + '&id=' + id;

}else{
window.location = '../Blog/Blog.php?id=".$_GET['id']."';
}

}else{
window.location = '../Blog/edit.php';
}
}
</script>";

当我选择<option value ='delete'>Delete</option>时它应该将我重定向到deleteBlog.php页面并删除所选entryID的内容。

删除Blog.php代码:

<?php
include("../Connection.php");

if(isset($_GET['entryID'])){


$user = $_GET['id'];
$entry = $_GET['entryID'];

mysqli_query($connect_db, "Delete from blog_tbl where Blog_ID=" .$entry);
header('Location: ../Blog/Blog.php?id='.$user);

}

?>

如有任何建议,我们将不胜感激。谢谢!

最佳答案

为此,您需要编写最少的 php,尤其是涉及 javascript 部分时。只需存储博客 ID(我将把它存储在 select 属性的名称中)并通过 JavaScript 提取。我将使用 jQuery 来做 JS 的事情。

<?php
# Include database
include("../Connection.php");
# Create a simple function that does not use id="downpng" (id values are
# supposed to be unique
function getOrderDropDown($con)
{
$query = "Select * from indexview order by Blog_ID Desc";
$postsql = mysqli_query($con,$query) or die('Connection unsuccessful');
$str = '';
while($rows=mysqli_fetch_array($postsql,MYSQL_ASSOC)){
$str .= "
<select name='downpng[".$rows['Blog_ID']."]' class='blog_select'>
<option value='void'></option>
<option value = 'edit'>Edit Blog</option>
<option value ='delete'>Delete</option>
</select>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​";
}
return $str;
}

# Write the selects to page
echo getOrderDropDown($connect_db);
?>

提取所选内容的 JavaScript:

<script>
// I would only do php here, use a special chars, otherwise you will be easily hacked by user input
var id = <?php echo (!empty($_GET['id']))? '"'.htmlspecialchars($_GET['id'],ENT_QUOTES).'"' : 'false' ?>;
// On change of this class type
$('.blog_select').on('change',function(e) {
// Get the name (which contains the id)
var row = $(this).attr('name').replace(/[^0-9]/gi,'');
// This will be the action (delete, edit)
var action = $(this).val();
// On delete, assign the actions and send values
if(action == 'delete'){
var redirect;
var con = confirm('Are you sure?');
if(con){
redirect = 'google.php?entryID='+row+'&id='+id;
}else{
redirect = '../Blog/Blog.php?id='+id;
}
}else{
redirect = '../Blog/edit.php';
}
// Just do one redirect
window.location = redirect;
});
</script>

关于javascript - 使用 PHP 和 Javascript 删除选定 ID 的选定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42058366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com