<?php
$courses_array = array(
'course_no' => array('E1','E2','E3'),
'course_title' => array('A', 'B', 'C'),
'course_credit' => array('1','2','3')
);
if( !empty($courses_array) ) {
echo "<input type='checkbox' name='select_all' id='select_all' onClick='select_all_courses(this); check_course_registration_level_complete();' /> Select all <span id='error'> Please Select Courses </span> <br />";
}
else {
echo '<b style="color:green;">There is no courses found for you. <br /> If this problem is not desired please contact with your Course Teacher.</b>';
}
foreach( $courses_array['course_no'] as $course ) {
?>
<input type='checkbox' name='course[course_no][]' id='course[course_no][]' onClick='check_course_registration_level_complete(); change_hidden_input(this);' value='<?php echo $course; ?>' /> <?php echo $course; ?>
<input type='hidden' name='course[course_title][]' disabled id='course[course_title][]' value='<?php echo $courses_array['course_title'][ array_search( $course, $courses_array['course_no'] ) ]; ?>'/>
<input type='hidden' name='course[course_credit][]' disabled id='course[course_credit][]' value='<?php echo $courses_array['course_credit'][ array_search( $course, $courses_array['course_no'] ) ]; ?>'/>
<input type='hidden' name='course[course_type][]' disabled id='course[course_type][]' value='<?php echo $courses_array['course_type'][ array_search( $course, $courses_array['course_no'] ) ]; ?>'/>
<?php
}
if( !empty($courses_array) ){
echo "<br /><br /><input type='submit' name='course_registration_complete' value='Submit'>";
}
?>
<script>
function select_all_courses(source){
var checkboxes = document.getElementsByName('course[course_no][]');
for(var i=0, n=checkboxes.length; i<n;i++) {
checkboxes[i].checked = source.checked;
document.getElementById('course[course_title][]').disabled = !(source.checked);
document.getElementById('course[course_credit][]').disabled = !(source.checked);
document.getElementById('course[course_type][]').disabled = !(source.checked);
}
}
</script>
<script>
function change_hidden_input(course){
if(course.checked){
document.getElementById('course[course_title][]').disabled = false;
document.getElementById('course[course_credit][]').disabled = false;
document.getElementById('course[course_type][]').disabled = false;
}else{
document.getElementById('course[course_title][]').disabled = true;
document.getElementById('course[course_credit][]').disabled = true;
document.getElementById('course[course_type][]').disabled = true;
}
}
</script>
<script>
function check_course_registration_level_complete(){
var checkboxes = document.getElementsByName('course[course_no][]');
var ok = false;
count = 0;
for(var i=0, n=checkboxes.length;i<n;i++) {
if( checkboxes[i].checked ) {
ok = true;
count++;
document.getElementById('error').style.display = 'none';
}
}
if( count == checkboxes.length ){
document.getElementById('select_all').checked = true;
}
else{
document.getElementById('select_all').checked = false;
}
if(!ok){
document.getElementById('error').style.display = 'block';
return false;
}
}
</script>