Php JQuery JSON到MySql插入

Php JQuery JSON到MySql插入,php,jquery,mysql,json,Php,Jquery,Mysql,Json,我有一个不带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

我有一个不带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:


尝试将
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
用于指定服务器返回的数据的格式


使用jquery,数据属性将转换为post数据。因此,您不会在php中使用
$\u POST['data']
,而是使用jsonData变量so$\u POST['cpinfo courseName']下的列名……等等。或者,如果您希望所有内容都在
$\u POST['data']
下,在javascript中,您可以执行如下操作:
数据:{'data':jsonData},
并在
数据
键下以对象文字形式包装js var jsonData


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

jsonData
不是有效的JSON。将其作为普通对象编写,并让jQuery进行转换
$.ajax({
  type: "POST",
  url: "../../trainingReporting.php",
  dataType:"application/json",
  data: jsonData,
  contentType: "application/json",
  success: function() {...