2012-02-07 86 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));