使用PHP和Javascript删除选定ID的选定行
我试图通过将参数传递到URL来删除选定ID的行。比方说,我有entryID 1和2,每当我尝试选择并删除条目1的内容时,它都会成功删除entryID 1的内容,但问题是当我选择删除entryID 2时,它仍然会删除entryID 1而不是2。我在考虑一个变量使用PHP和Javascript删除选定ID的选定行,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我试图通过将参数传递到URL来删除选定ID的行。比方说,我有entryID 1和2,每当我尝试选择并删除条目1的内容时,它都会成功删除entryID 1的内容,但问题是当我选择删除entryID 2时,它仍然会删除entryID 1而不是2。我在考虑一个变量var row='“$rows['Blog_ID']”的内容不会更改,仅保留entryID 1的值,即使我选择了其他值 这是我到目前为止试过的 <?php include("../Connection.php"); $post_quer
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>";
如有任何建议,将不胜感激。谢谢 为此,您需要使用最少的php,尤其是在javascript部分。只需存储blog 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从html元素提取id,而不是使用php在循环中生成一堆js函数/脚本。您应该有一个js函数(不是由php创建的),从html元素获取id,并仅使用一个函数提交它。
<?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);
?>
<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>