Currently, I am developing a reservation website using fullcalendar. I am facing an issue where the success function in my Ajax request is not working even though all the data is being successfully inserted into the database. I also tried changing 'success' to 'error', but still no response. Below is the code snippet where I insert an event into the database:
select: function(start, end, allDay)
{
var teacher = prompt("Enter ResourceID");
if(teacher)
{
var start = $.fullCalendar.formatDate(start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss");
$.ajax({
url:"http://show981111.cafe24.com/login-system/addevent.php",
type:"POST",
data:{userName: '<?php echo $name; ?>' , newlyBookedDate:start, courseTeacher : teacher, userBranch:'<?php echo $userBranch; ?>', userID: '<?php echo $userID; ?>'},
success:function()
{
calendar.fullCalendar('refetchEvents');
alert("Added Successfully");
}
})
}
},
The following is where I retrieve the PHP variables used in the above code:
<?php
session_start();
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: error.php");
}
else {
// Makes it easier to read
$name = $_SESSION['userName'];
$userBranch = $_SESSION['userBranch'];
$userID = $_SESSION['userID'];
}
?>
The below code snippet pertains to addevent.php:
<?php
// Values received via ajax
$userName = $_POST['userName'];
$newlyBookedDate = $_POST['newlyBookedDate'];
$courseTeacher = $_POST['courseTeacher'];
$userBranch = $_POST['userBranch'];
$userID = $_POST['userID'];
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=show981111', 'show981111', 'pass');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
// insert the records
$sql = "INSERT INTO DAYSCHEDULE (newlyBookedDate, userID, userName, courseTeacher,userBranch ) VALUES (:newlyBookedDate, :userID, :userName, :courseTeacher, :userBranch)";
$q = $bdd->prepare($sql);
$q->execute(array(':newlyBookedDate'=>$newlyBookedDate, ':userID'=>$userID, ':userName'=>$userName, ':courseTeacher'=>$courseTeacher,':userBranch'=>$userBranch ));
?>