I was trying to insert data from dataTable into a related table in the database using PHP, jQuery and AJAX. I don't know where am missing it out... Please I need you to help me solve this problem. The problem am having there is to register those courses in table.
Code is given below
PHP -- enroll.php
<?php
if(isset($_POST['register'])){
$id = $_POST['register'];
$matricNum = $_SESSION['matricNum'];
$sqlregister = "INSERT INTO courseenrolls (matricNum, course) VALUES ((SELECT matricNum FROM student WHERE matricNum = '$matricNum'), (SELECT courseID FROM course WHERE courseID = '$id'))";
$result_register = mysqli_query($db, $sqlregister);
if($result_register){
echo '<script >window.location.href="enroll.php"</script>';
}
else{
echo '<script>alert("already register")</script>';
}
}
?>
coursereg.php
<?php
//coursereg.php
$db = mysqli_connect('localhost', 'root', 'password', 'asg');
$column = array("course.courseID", "course.courseCode", "course.courseName", "course.courseUnit");
$query = "
SELECT * FROM level
INNER JOIN course
ON level.levelID = course.level
";
$query .= " WHERE ";
if(isset($_POST["is_level"]))
{
$query .= "course.level = '".$_POST["is_level"]."' AND";
}
$data = array();
while($row = mysqli_fetch_array($result))
{
$sub_array = array();
$sub_array[] = $row["courseID"];
$sub_array[] = $row["courseCode"];
$sub_array[] = $row["courseName"];
$sub_array[] = $row["courseUnit"];
$sub_array[] = '<a href="enroll.php?register='.$row['courseID'].'" type="button" name="register" class="btn btn-success btn-xs success" id="register" onclick="return confirm(\'Are you sure?\')"">Register</a>';
$data[] = $sub_array;
}
function get_all_data($db)
{
$query = "SELECT * FROM course";
$result = mysqli_query($db, $query);
return mysqli_num_rows($result);
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => get_all_data($db),
"recordsFiltered" => $number_filter_row,
"data" => $data
);
echo json_encode($output);
?>
jQuery and Ajax -- enroll.php
$(document).ready(function(){
load_data();
function load_data(is_level)
{
var dataTable = $('#course_data').DataTable({
"processing": true,
"serverSide": true,
"order":[],
"ajax":{
url:"coursereg.php",
type:"POST",
data:{is_level:is_level}
},
"columsDefs":[
{
"targets":[2],
"orderable":false,
},
],
});
}
$(document).on('change', '#level', function(){
var level = $(this).val();
$('#course_data').DataTable().destroy();
if(level != '')
{
load_data(level);
}
else
{
load_data();
}
});
// $(document).
});
</script>
The whole essence of it is that i want students (Tbl: Student) to register course (Tbl: Course) from the DataTable which will be inserted into courseEnrolls (Tbl: CourseEnrolls).
Comments
Post a Comment