簡體   English   中英

jQuery JSON到MySql插入

[英]JQuery JSON to MySql Insert

我有一個沒有LMS托管的Captivate培訓,但是我想從培訓中發送數據。 我已經編寫了JavaScript以建立變量並將其發布到php頁面,但是我無法通過發布獲取請求。 想法?

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";
}

嘗試將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 application/x-www-form-urlencoded; charset=UTF-8作為格式,並且您希望它為json。

dataType用於指定服務器返回的數據的格式。

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

使用jquery,data屬性將轉換為發布數據。 因此您不會在php中使用$_POST['data'] ,但會在jsonData變量下使用列名。 所以$ _POST ['cpInfoCourseName'] ...等等。 或者,如果您希望所有內容都在$_POST['data'] ,則可以在javascript中執行以下操作: data:{'data':jsonData},並將js var jsonData封裝在data鍵下的對象常量中。

您還可以運行print_r($ _ POST)以顯示$ _POST中的所有數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM