0

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.

2
  • 2
    You're reloading the page before the ajax call completes (wild guess as you have'nt really told us what the problem is, nor asked a question). Commented Dec 7, 2012 at 11:21
  • 1
    It looks like you are using the same id for all checkboxes. You can't do that. Ids need to be unique. You can use the same 'coursesToAdd[]' for all names, but not ids. Use a class instead and then loop over getElementsByClassName instead of getElementById. Commented Dec 7, 2012 at 11:33

3 Answers 3

1

As I mentioned in my comment above:

Without even looking at the AJAX code you posted, you are having a problem with the way you get the values from the checkboxes. You are using the same id coursesToAdd[] to all checkboxes. That is invalid, as all ids need to be unique on the page. You need to use a classname instead.

In your HTML/PHP:

while ($courses) {
    echo "<td><input type='checkbox' name='coursesToAdd[]' class='coursesToAdd' value='$courses[0]'>$courses[0]</td></tr>";            
}

And in your JS you can get to the checkboxes like this:

var courseCheckboxes = document.getElementsByClassName("coursesToAdd");
for (var i=0; i < courseCheckboxes.length; i++) {
    if (courseCheckboxes[i].checked) {
       // do something with the checkbox
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are doing it async. So you should make use of onreadystatechange event.

Try:

xmlhttp.onreadystatechange = function(){
    window.location.reload();       
}

And remove the location.reload()

1 Comment

The problem seems to be with getting the values from the checkboxes that the user checks. Im not sure what the best way to access this information is from javascript
0

When having more than one values to pass through POST it requires encodeURIComponent(variablename)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.