g,一天。有人可以幫助我理解爲什麼我的代碼不會將結果返回給json嗎?我確信在我的代碼中有一個錯誤,但似乎無法找到它。應該發生的是$ dept和$ box的值應該在警報中返回,但這不會發生。感謝json不返回數據
<?php
function runSQL($rsql) {
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "sample";
$connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
$db = mysql_select_db($dbname);
$result = mysql_query($rsql) or die ('test');
return $result;
mysql_close($connect);
}
$new = 1;
$items = rtrim($_POST['items'],",");
$sql = "SELECT * FROM `boxes` WHERE id IN ($items)";
$result = runSQL($sql);
$i = 0;
$rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($i < $rows) {
$dept .= $row['department'] . "," ;
$box .= $row['custref'] . "," ;
} else {
$dept .= $row['department'];
$box .= $row['custref'];
}
$i++;
}
/*$items = rtrim($_POST['items'],",");
$sql = "UPDATE `boxes` SET status = 'Deleted' WHERE id IN ($items)";
$result = runSQL($sql);*/
//$sql = "INSERT INTO `act` (`item`) VALUES (\''.$box.'\')";
//$result = runSQL($sql);
$total = count(explode(",",$items));
$result = runSQL($sql);
$total = mysql_affected_rows();
/// Line 18/19 commented for demo purposes. The MySQL query is not executed in this case. When line 18 and 19 are uncommented, the MySQL query will be executed.
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: text/x-json");
$json = "";
$json .= "{\n";
$json .= "dept: '".$dept.",'\n";
$json .= "box: '".$box."'\n";
$json .= "}\n";
echo $json;
?>
阿賈克斯
success: function(data){
dept = data.dept;
box = data.box;
alert("You have successfully deleted\n\r\n\rBox(es): "+data.dept+data.box);
$("#flex1").flexReload();
}
** **爲什麼你產生JSON自己,而不是使用幾十個擴展或庫那會爲你的嗎? – 2010-09-27 13:30:06
習慣:-)我真的因爲時間限制需要在這段代碼中找到錯誤。謝謝 – 2010-09-27 13:34:05