我很难对复选框进行验证,但似乎无法理解它出错的地方。任何帮助都会很棒。因为它作为一个数组从MySQL中拉出来,我不确定这是否是我错的地方。
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
if(!form.check_box_delete[].checked) {
alert('Please check at least one of the options.');
return false;
}
return true;
}
</script>
以下是正文的代码:
<form name="form" method="post" action="loan_checkedIn.php" onsubmit="return checkform(this);">
<?php
echo "<table border='1' align='center' width='900'>
<tr>
<th bgcolor='#00a3e0'><font face='Arial'>Select</font></th>
<th bgcolor='#00a3e0'><font face='Arial'>Customer</font></th>
<th bgcolor='#00a3e0'><font face='Arial'>Make</font></th>
<th bgcolor='#00a3e0'><font face='Arial'>Model</font></th>
</tr>";
$results = mysql_query("SELECT * FROM loan WHERE email='$email' AND status='Out'");
while($row1 = mysql_fetch_array($results))
{
echo "<tr>";
echo "<td align='center'><input type='checkbox' name='check_box_delete[]' value='" . $row1['id'] . "'></td>";
echo "<td align='center'><font face='Arial'>" . $row1['customer'] . "</font></td>";
echo "<td align='center'><font face='Arial'>" . $row1['make'] . "</font></td>";
echo "<td align='center'><font face='Arial'>" . $row1['model'] . "</font></td>";
echo "</tr>";
}
echo "</table>";
?>
<p align="center"><input type="submit" style="background-color:#00A3E0; color:#FFFFFF;" name="submit" value="Return Equipment"></p>
</form>
答案 0 :(得分:0)
您可以遍历表单中的所有元素i
。
j
答案 1 :(得分:0)
你在javascript中有问题。更改您的代码如下:
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
var checkboxs=form['check_box_delete[]'];
var cheked=false;
for(var i=0,l=checkboxs.length;i<l;i++)
{
if(checkboxs[i].checked)
{
cheked=true;
break;
}
}
if(!cheked)
{
alert('Please check at least one of the options.');
return false;
}
return true;
}
</script>
也停止使用deprected / removed mysql _ 扩展名。使用 mysqli_ 或 PDO (使用准备好的查询)