用json发送字符串到php

时间:2012-07-27 11:56:48

标签: php mysql ajax json

我的代码工作得很好除了“userName”因为某些原因通过JSON发送字符串不会发布到表,它什么都不发送。

有人能看出问题所在吗?

jquery的

lowestScoreId = 1;
userPoints = 50;
userName = "ted";

$.getJSON("functions/updateHighScores.php", {lowestScoreId: lowestScoreId, userPoints: userPoints, userName: userName}, function(data) {

  $('#notes').text(data.userName); //for testing

}); 

PHP

lowestScoreId =  json_decode($_GET['lowestScoreId']);
$userName =  json_decode($_GET['userName']);
$userPoints =  json_decode($_GET['userPoints']);

include 'config.php';

$currentTime = time();

mysql_query("UPDATE highScores
SET `name`    = '$userName',
    `score`   = '$userPoints',
    `date`    = '$currentTime'
WHERE id='$lowestScoreId'");

echo json_encode(array("userName" => $userName));  // for testing

1 个答案:

答案 0 :(得分:2)

你为什么用这个:

$userName = $obj = json_decode($_GET['userName']);

它正常工作

$userName = $_GET['userName'];