我有一些我生成的复选框值
$sql = "sql statement";
$courseResult = mysqli_query($connection, $sql);
$courses = mysqli_fetch_row($
// Display the courses the user has taken
while ($courses) {
echo "<td><input type='checkbox' name='coursesToAdd[]' id='coursesToAdd[]' value='$courses[0]'>$courses[0]</td></tr>";
$courses = mysqli_fetch_row($courseResult);
}
<input type='button' onclick='EditStudentCertificates()' value='Submit'/>
我想要做的是获取用户签入数组的值,然后从我试图这样做的javascript访问这个数组,但它似乎不起作用:
function EditStudentCertificates(){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var coursesToAddField = document.getElementById("coursesToAdd[]");
var coursesToAdd = coursesToAddField ? coursesToAddField.value : '';
xmlhttp.open("POST","updateStudentCInfo.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("certValue="+certValue+"&coursesToAdd="+coursesToAdd);
window.alert("Your certificate information has been updated");
window.location.reload();
}
我认为问题出在我试图从 javascript 中的复选框中获取值的方式上,可能是:
var coursesToAddField = document.getElementById("coursesToAdd[]");
var coursesToAdd = coursesToAddField ? coursesToAddField.value : '';
因为变量 courseToAdd 似乎只包含页面上第一个复选框的值,无论我在点击提交之前在页面上检查了多少个复选框。