I've been trying to call saveTaskDB
function from php using ajax. It will return as success but the database is not updated. I have tried creating a 'create.php'
with just the saveTaskDB
function code in it and when I called the file 'directly', it returns as success plus it will update my database. I don't know why I cannot call a specific function in my php and I also want to know how store my php functions to a single php file and call them via AJAX. Thank you for anyone that can help me.
I have my codes like this
tasklogger.js:
function saveTask(){
$.ajax({
method: "POST",
url:"../_tasklogger/classes/tasklog.php", //the page containing php script
data:{ action: 'add',
date: $("#date").val(),
taskName: $("#taskName").val(),
taskType: $("#taskType").val(),
duration: $("#duration").val(),
startTime: $("#startTime").val(),
endTime: $("#endTime").val()
},
success: function(data){
goSuccess();
}
});
}
tasklog.php
require_once 'dbconfig.php';
if (isset($_POST['action']) && !empty($_POST['action'])){
$date= $_POST['date'];
$taskName= $_POST['taskName'];
$taskType= $_POST['taskType'];
$duration= $_POST['duration'];
$startTime= $_POST['startTime'];
$endTime= $_POST['endTime'];
saveTaskDB($date, $taskName, $taskType, $duration, $startTime, $endTime);
}
function saveTaskDB($date, $taskName, $taskType, $duration, $startTime, $endTime){
try{
$stmt = $db_con->prepare("INSERT INTO tasks (TaskDate,TaskName,TaskType,Duration,StartTime,EndTime) VALUES(:tdate, :tname, :ttype, :dur, :stime, :etime)");
$stmt->bindParam(":tdate", $date);
$stmt->bindParam(":tname", $taskName);
$stmt->bindParam(":ttype", $taskType);
$stmt->bindParam(":dur", $duration);
$stmt->bindParam(":stime", $startTime);
$stmt->bindParam(":etime", $endTime);
if($stmt->execute()){
echo 'alert("success")';
}else{
echo 'alert("wrong")';
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
create.php
require 'dbconfig.php';
$date= $_POST['date'];
$taskName= $_POST['taskName'];
$taskType= $_POST['taskType'];
$duration= $_POST['duration'];
$startTime= $_POST['startTime'];
$endTime= $_POST['endTime'];
try{
$stmt = $db_con->prepare("INSERT INTO tasks (TaskDate,TaskName,TaskType,Duration,StartTime,EndTime) VALUES(:tdate, :tname, :ttype, :dur, :stime, :etime)");
$stmt->bindParam(":tdate", $date);
$stmt->bindParam(":tname", $taskName);
$stmt->bindParam(":ttype", $taskType);
$stmt->bindParam(":dur", $duration);
$stmt->bindParam(":stime", $startTime);
$stmt->bindParam(":etime", $endTime);
if($stmt->execute()){
return true;
}else{
return false;
}
}
catch(PDOException $e){
echo $e->getMessage();
}