2012-02-07 83 views
0

我有下面的代碼..訪問JSON對象通過PHP

if (config.sendResultsURL !== null) 
{ 
    console.log("Send Results"); 
    var collate =[]; 
    for (r=0;r<userAnswers.length;r++) 
    {     
    collate.push('{"questionNumber'+parseInt(r+1)+ '"' + ': [{"UserAnswer":"'+userAnswers[r]+'", "actualAnswer":"'+answers[r]+'"}]}'); 
    } 
    $.ajax({ 
    type: 'POST', 
    url: config.sendResultsURL, 
    data: '[' + collate.join(",") + ']', 
    complete: function() 
    { 
     console.log("Results sent"); 
    } 
    }); 
} 

用Firebug我得到這個從控制檯。

[{"questionNumber1": [{"UserAnswer":"3", "actualAnswer":"2"}]},{"questionNumber2": [{"UserAnswer":"3", "actualAnswer":"2"}]},{"questionNumber3": [{"UserAnswer":"3", "actualAnswer":"2"}]},{"questionNumber4": [{"UserAnswer":"3", "actualAnswer":"1"}]},{"questionNumber5": [{"UserAnswer":"3", "actualAnswer":"1"}]}] 

從這裏腳本將數據發送到emailData.php讀取...

$json = json_decode($_POST, TRUE); 
$body = "$json"; 
$to = "[email protected]"; 
$email = 'Diesel John'; 

$subject = 'Results'; 
$headers = "From: $email\r\n"; 
$headers .= "Content-type: text/html\r\n"; 

// Send the email: 
$sendMail = mail($to, $subject, $body, $headers); 

現在我得到了電子郵件但它是空白。

我的問題是我如何將數據傳遞給emailData.php並從那裏訪問它?

+0

這個鏈接將幫助您找出解決辦法:http://stackoverflow.com/questions/2237601/how-to-get-the-post-values-from-serializearray-in-php – 2012-02-07 12:30:17

+0

我真的希望它做到了!我只是不明白!也許我一直在盯着這太久了! – dieseljohn 2012-02-07 15:44:53

回答

0

如果你解碼你的JSON,你將有一個散列,而不是一個字符串。如果你想郵寄你一樣印在控制檯上什麼,只是這樣做:

$body = $_POST['data'];

另一種選擇是解析JSON成PHP哈希和的var_dump說:

$json = json_decode($_POST['data'], TRUE); 
$body = var_export($json, TRUE); 
+0

我試過這個,但是發送的郵件中包含「Null」 – dieseljohn 2012-02-07 14:25:11

+0

試着將我的答案和Shiplu的答案結合起來! – Jochem 2012-02-07 15:01:19

0

json_decode將字符串轉換爲對象。 只需執行下面的代碼並檢查值。

print_r($json) 

直接將json對象分配給字符串,這是非常糟糕的。

+0

你的建議將回顯$ json結構。只有輸出緩衝才能被捕獲。另外「$ json」在PHP中不是很糟糕。 $ json沒有解釋或任何東西。當$ json是一個數組/對象時,它是毫無意義的(對於大多數用途而言),但它並不壞。 – Jochem 2012-02-07 12:35:07

+0

不,我是說,不要直接做這個$ body =「$ json」;因爲$ json是對象而不是字符串。 – 2012-02-07 16:34:46

+0

因爲你是對的!我的觀點是,稱這種「非常糟糕」有點誇張。 – Jochem 2012-02-08 08:43:24

2
  1. 創建要傳遞給PHP
  2. 使用JSON.stringify(),使該對象的JSON字符串的對象。
  3. 使用POST或GET將其傳遞給PHP腳本,並使用名稱
  4. 根據您的請求從$_GET['name']$_POST['name']捕獲它。
  5. 在php中應用json_decode以獲取JSON作爲本機對象。

在你的情況下,你可以通過userAnswers [r]和答案[r]。數組序列被保留。

在用於循環使用,

collate.push({"UserAnswer":userAnswers[r], "actualAnswer":answers[r]}); 

在AJAX請求使用,

data: {"data" : JSON.stringify(collate)} 

在PHP端,

$json = json_decode($_POST['data'], TRUE); // the result will be an array. 
+0

我試過這個,發送的郵件只包含'1',但我可以在控制檯的響應選項卡中看到數組。 – dieseljohn 2012-02-07 14:19:35

0

使用下面的JavaScript代碼

var collate =[], key, value; 
for (r=0;r<userAnswers.length;r++) { 
    key = questionNumber + parseInt(r+1);    
    value = { "UserAnswer": userAnswers[r], "actualAnswer": answers[r] }; 
    collate.push({ key : value }); 
} 
$.post(config.sendResultsURL, { data: collate }, function(data) { 
    console.log("Results sent"); 
}); 

而做到這一點在PHP

$data = json_decode($_POST['data'], true); 

,你將與陣列中的所有數據。

0
$jsonData = file_get_contents('php://input'); 
$json = json_decode($jsonData, 1); 
mail('email', 'subject', print_r($json, 1));