gpt4 book ai didi

javascript - OnClick 按钮仅给出 ajax jquery php mysqli 中的第一行值

转载 作者:行者123 更新时间:2023-11-30 00:42:55 26 4
gpt4 key购买 nike

当我点击按钮(id=button)时,它只给我隐藏的第一行值(输入“id=close”)..

我有数组列表,但是当我单击关闭按钮时,所有它都给出第一行 id ..为什么会这样

下面是我的代码文件:

echo "<script type=\"text/javascript\">
$(document).ready(function(){
$('input[id=button]').on('click delegate keypress ', function(e){
e.preventDefault();
var id= $('input[name=close]').val();
var mainid = id;
alert(mainid);
$.ajax({
type:\"get\",
url:\"test.php\",
data:{
id:mainid
},
success:function(data){
if(data){
alert('Close Sucessful');
}else {
alert('Something Went Wrong');
}
}
});
});
});
</script>";
<小时/>
function processdasa($row){
$a =" <input type=\"hidden\" name='close' value=\"".$row['id']."\" id=\"close\">
<input type=\"button\" value=\"".$row['id']."\" id=\"button\">
";
return $a;
}

测试.php

<?php
$con= mysqli_connect("localhost","root","ght");
$db = mysqli_select_db($con, "abc");
$id = $_GET["id"];
mysqli_query($con , "UPDATE testtable SET closed=1 WHERE id=$id ");
mysqli_close($con);
?>

有关更多说明,这是我的列表:

$result = get_list();
if($result){
$cols = array(
_("id") => array('align'=>'center'),
_("Name") => array('align'=>'center'),
_("Closed") => array('align'=>'center','function'=>'processdasa')
); }

因此它看起来像:

Id |  Name |   Closed
------------- ----------------
1 | ABC | 1 (BUTTON)
2 | RF | 2 (BUTTON)
3 | GF | 3 (BUTTON)
4 | GF | 4 (BUTTON)

最佳答案

如果 $row['id'] 是 id 数组,那么您必须进行这些更改在此函数中更改名称 close 以将 id 数组携带为 close[]

function processdasa($row){
$a =" <input type=\"hidden\" name='close[]' value=\"".$row['id']."\" id=\"close\">
<input type=\"button\" value=\"".$row['id']."\" id=\"button\">
";
return $a;
}

现在在你的 test.php 中

<?php
$con= mysqli_connect("localhost","root","ght");
$db = mysqli_select_db($con, "abc");
$id = $_GET["id"];
//$id is an arry so first implode it
$new_id=implode(",",$id);
//and now in your query us IN keyword
mysqli_query($con , "UPDATE testtable SET closed=1 WHERE id IN($new_id) ");
mysqli_close($con);
?>

关于javascript - OnClick 按钮仅给出 ajax jquery php mysqli 中的第一行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625886/

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