1问题:数据库中的变量chk不是在说多个值 2问题:删除变量没有得到正确的值,即患者ID 我想使用复选框删除多行 数据库代码:
function delete()
{
$con = mysqli_connect("localhost","root","","rishita_db");
$sql="select * from 14_patientdetails";
$result=mysqli_query($con,$sql);
?>
<center>
<h1><u>Patient Details</u></h1>
<table border="1" style="font-family:Georgia;color:#800000;font-style:bold;">
<tr style="font-family:Georgia;color:green;font-style:bold;">
<th>#</th>
<th>Patient ID</th>
<th>Patient Name</th>
<th>DOB</th>
<th>Gender</th>
<th>Address</th>
<th>Phone No.</th>
<th>Medicare</th>
<th>Doctor Associated</th>
</tr>
<form method="post" action="delete.php">
<?php
while($row=mysqli_fetch_array($result))
{
$r=$row['patientId'];
?>
<tr>
<td><input type='checkbox' name='checkbox[]' id="checkbox[]" value=<?php echo $r; ?>></td>
<td><?php echo $row['patientId']; ?></td>
<td><?php echo $row['patientName']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Gender']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Phone']; ?></td>
<td><?php echo $row['Medicare']; ?></td>
<td><?php echo $row['Doctor']; ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="del" type="button" onClick='myFunction()' id="del" value="Delete"></td>
</tr>
</table>
<span id="msg"></span>
</form>
<script language="javascript">
function myFunction() {
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
else
{
xmlhttp=new Activexobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("msg").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","delete.php",true)
var chk=document.getElementById("checkbox[]").value;
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("chk="+chk); }
</script>
<?php
}
服务器端:
$chk=$_POST['chk'];
echo "Enter".$chk;
$chkcount = count($chk);
echo"<br>count:".$chkcount;
$del=$chk;
echo 'del:'.$del;
for($i=0;$i<$chkcount;$i++)
{
$delete=$del[$i];
echo "delete: ".$delete;
$con = mysqli_connect("localhost","root","","rishita_db");
$sql1 = "DELETE FROM 14_patientdetails WHERE patientId='$delete'";
$q = mysqli_query($con,$sql1);
}
if($q)
{
echo "Success";
}
else{echo 'Fail';}
?>
答案 0 :(得分:0)
我担心你只会使用document.getElementById("checkbox[]")
我建议添加一个容器并找到其中所有选定元素的值,或者可能会搜索该名称
<script language="javascript">
function myFunction() {
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
else
{
xmlhttp=new Activexobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("msg").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","delete.php",true)
var chk=document.getElementsByName("checkbox[]");
// buffer values into array
var qAry = [];
for (var x in chk) {
if (chk[x].checked) {
qAry.push('chk[]='+chk[x].value);
}
}
// merge
qString = qAry.join('&');
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(qString); }
</script>
我已经让你在fiddle中运行了(不幸的是没有ajax请求)