Reputation: 439
The saveTask()
function calls requestJSONAJAX
, but the requestJSONAJAX
function returns false
. This happens even though the query on my tasklog.php
is successful; it even updates my database. I don't know where my error originates. Is it in my tasklog.php
or in the JavaScript functions, or am I using the return
wrong?
function saveTask(){
if(requestJSONAJAX('add')){
goSuccess();
}else{
alert('error');
}
}
function requestJSONAJAX(action){
var newObj;
if(action == "add"){
newObj = { action: "add",
date: $("#date").val(),
taskName: $("#taskName").val(),
taskType: $("#taskType").val(),
duration: $("#duration").val(),
startTime: $("#startTime").val(),
endTime: $("#endTime").val()
}
}else if(action == "login"){
newObj = { action: "login",
username: $("#username").val(),
password: $("#password").val()
}
}
$.ajax({
method: "POST",
url:"../_tasklogger/classes/tasklog.php", //the page containing php script
dataType: 'json',
data: newObj,
success: function(data){
status = data;
if(status == "success"){
return true;
}else{
return false;
}
},
error: function (req, status, err) {
console.log('Something went wrong', status, err);
}
});
}
tasklog.php
<?php
require_once 'dbconfig.php';
if(empty($_POST['action'])){
return;
}
if(($_POST['action']) != "getData"){
$date= $_POST['date'];
$taskName= $_POST['taskName'];
$taskType= $_POST['taskType'];
$duration= $_POST['duration'];
$startTime= $_POST['startTime'];
$endTime= $_POST['endTime'];
}
switch($_POST['action']){
case "add":
$sql = "INSERT INTO tasks (taskDate,taskName,taskType,duration,startTime,endTime,userId) VALUES(:tdate, :tname, :ttype, :dur, :stime, :etime, 1)";
$stmt = $db_con->prepare($sql);
$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 json_encode("success");
}else{
echo json_encode("error");
}
break;
case "getData":
break;
case "delete":
/* .... some code .... */
break;
case "update":
/* some code ...
notice there is no "break" here... and execution continues to the next case.... falls-thru */
default:
return;
}
?>
Upvotes: 1
Views: 147
Reputation: 44831
You don't return anything from requestJSONAJAX
. You return a boolean from your success
handler in your $.ajax()
call, but that's irrelevant. That doesn't make the function containing that $.ajax()
call—that is, requestJSONAJAX
—return anything.
It seems like you're trying to return a value from requestJSONAJAX
based on your AJAX call's success or failure. That's impossible; that's why it's called "AJAX" (Asynchronous JavaScript and XML). requestJSONAJAX
will (essentially) always finish before the AJAX call does. Even when it doesn't finish first, it's not waiting on the AJAX call, and it has no way of telling whether the call succeeded or failed.
To capture whether the call was successful, you'd need to set a flag somewhere (like a global variable). Even better, just handle whatever you want to do within that success
handler. Don't worry about returning a value.
Upvotes: 1