0

我有一个没有 LMS 的 Captivate 培训,但我想从培训中发送数据。我已经编写了 javascript 来建立变量并发布到 php 页面,但我无法通过 post 获取请求。想法??

Javascript:

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();

var myMovie = CaptivateController("Captivate");
//Retrieves the author's name, if available
var cpInfoCourseName = myMovie.query("cpInfoCourseName");
var currentDate = year + "-" + month + "/" + day;
var cpQuizInfoPassFail = myMovie.query("cpQuizInfoPassFail"); //1 = pass; 0 = fail
var cpQuizInfoPointsscored = myMovie.query("cpQuizInfoPointsscored");
var cpQuizInfoQuizPassPercent = myMovie.query("cpQuizInfoQuizPassPercent");
var cpQuizInfoTotalCorrectAnswers = myMovie.query("cpQuizInfoTotalCorrectAnswers");

var jsonData = "{'cpInfoCourseName': cpInfoCourseName, 'currentDate': currentDate, 'cpQuizInfoPassFail': cpQuizInfoPassFail, 'cpQuizInfoPointsscored': cpQuizInfoPointsscored,'cpQuizInfoTotalCorrectAnswers':cpQuizInfoTotalCorrectAnswers}";

$.ajax({
  type: "POST",
  url: "../../trainingReporting.php",
  dataType:"application/json",
  data: jsonData,
  success: function() {
        //alert("Your training results were saved and sent to your regional office.");
        //location.href = 'http://www.occ-connect.org/seconnect/occTrainings.php';           
    },
  error: function() {
                alert("Your data was not submitted");
    }
});

PHP:

    if(isset($_POST['data'])){
    $jsonData = $_POST['data'];

    //get region, email and ministry position of user
    $userInfo = mysql_query("SELECT profilevalue_8,profilevalue_9 FROM se_profilevalues WHERE profilevalue_user_id = '".$user->user_info['user_id']."'");

    while($r = mysql_fetch_array($userInfo))
    {
        $region = $r['profilevalue_9'];
        $ministry_position = $r['profilevalue_8'];
    }

    $user_id = $user->user_info['user_id'];
    $user_email = $user->user_info['user_email'];

    mysql_query("INSERT INTO trainingReporting (json,user_email,user_id,region,ministry_position) VALUES ('$jsonData','$user_email','$user_id','$region','$ministry_position')") or die('INSERT FAILED: '. mysql_error());
    echo "success!";

} else {
    echo "error";
}
4

2 回答 2

0

尝试添加contentType到您的 ajax 调用。

$.ajax({
  type: "POST",
  url: "../../trainingReporting.php",
  dataType:"application/json",
  data: jsonData,
  contentType: "application/json",
  success: function() {...

默认情况下,jQuery 使用application/x-www-form-urlencoded; charset=UTF-8格式并且您希望它是 json。

dataType是指定服务器返回的数据的格式。

http://api.jquery.com/jQuery.ajax/

于 2013-01-09T21:16:21.513 回答
0

使用 jquery,数据属性被转换为发布数据。所以你不会$_POST['data']在 php 中使用,而是在你的 jsonData var 下使用列名。所以 $_POST['cpInfoCourseName']...等等。或者,如果您希望所有内容都$_POST['data']在 javascript 中,您可以执行以下操作:data:{'data':jsonData},并将 js var jsonData 包装在data键下的对象文字中。

您还可以运行 print_r($_POST) 以显示 $_POST 中的所有数据。

于 2013-01-09T21:16:40.487 回答