doubi2228 2017-04-09 02:55
浏览 50
已采纳

无法使用ajax调用特定的php函数

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();
}
  • 写回答

1条回答 默认 最新

  • dongxi5494 2017-04-09 03:17
    关注

    I'm not sure how deep this question goes. You may need to review the relationship between JavaScript and PHP, or basic web service design patterns. I say this because I do not know what your big picture goals are. If you want a basic script that triggers functions, there's thousands of ways to do that.

    dataType: can be default: Intelligent Guess (xml, json, script, or html)

    Your create.php is returning TRUE and FALSE

    Your tasklog.php is returning JavaScript alert()

    These are different types. Your calling script tasklogger.js doesn't do anything with the response.

    • I HOPE YOU SCRUB YOUR DATA!*

    To trigger arbitrary functions in php, you could evaluate some parameter that you specify in the JavaScript request. Let's call the variable... action.

    In PHP you can now evaluate for the action...

    if(empty($_POST['action'])){
    return;
    }
    switch($_POST['action']){
        case "save":
            $some_var = $_POST['somearg'];
            $some_other = $_POST['another_arg'];
            save($some_var, $some_other);
            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 */
        case "create":
              /* some code */
              break;
        default:
             /* do something else */
    }
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵