I am having some trouble trying to retrieve data from MYSQL using the options checked off in a checklist.
I have 2 tables in MYSQL, 1st for degree_names - which are outputted automatically as a checklist, and 2nd for courses related to each distinct degree name. Both these tables are relational, i.e. they are connected such that "courses" is the child table of "degree_names".
So my question is... What can I do to change my code so that the options(2 or more) that I check off in the checklist connect to my "degree_names" table and then fetch all the courses related to those degrees from the "courses" table?
Here is my code so far which outputs a checklist of all the degrees directly from the degree_name table
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "major_degrees";
$str='';
// Create connection
$conn = new mysqli($hostname, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT degree FROM degree_names";
$result = $conn->query($sql);
$out = '';
$cnt = 0;
if ($result->num_rows > 0) {
// output data of each row from degree_names database as a checklist
while($row = $result->fetch_assoc()) {
$cnt++;
$out .= '<input id="cb_' .$cnt. '" class="checkChange" type="checkbox" name="check" value="ch" id="checky" />' .$row['degree']. '<br/>';
}
echo $out;
}
?>
</b>
</br>
<input class="btn-checkout" type="submit" value="Submit" id="submitBtn" disabled="disabled"/>
</div>
</div>
</form>
</body>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
$('.checkChange').change(function() {
var count = 0;
var len = $("[name='check']:checked").length;
// keep a counter for how many boxes have been checked
$('#checkboxes input:checked').each(function() {
count++;
});
// if 2 boxes are checked off then disable the rest
if (count == 2) {
$('#checkboxes input:not(:checked)').each(function() {
$(this).attr("disabled", true);
});
}
// else keep other options enabled
else {
$('#checkboxes input:not(:checked)').each(function() {
$(this).removeAttr('disabled');
});
}
//if exactly 2 boxes are checked off then enable the submit button, or else keep is disabled
if ($(this).is(":checked") && count == 2) {
$("#submitBtn").removeAttr("disabled");
} else {
$("#submitBtn").attr("disabled", "disabled");
}
});
</script>
</html>