I have some checkbox values that I generate as
$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'/>
what i want to do is get the values that the user checks into an array, and than access this array from javascript which i was trying to do as such, but it does not seem to work:
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();
}
What i think the problem is is something with the way i trying to get the values from the checkboxs in the javascript, possibly something with:
var coursesToAddField = document.getElementById("coursesToAdd[]");
var coursesToAdd = coursesToAddField ? coursesToAddField.value : '';
Because the variable coursesToAdd doesnt seem to hold anything but the value of the first checkbox on the page no matter how many checkboxes i check on the page before hitting submit.