這是保存爲db2.php我的JSON,PHP不工作
<?php
mysql_connect("mysql15.000webhost.com","a3189966_root","");
mysql_select_db("a3189966_pd");
$firstName = $_GET[firstName];
echo $firstName;
$q=mysql_query("SELECT * FROM people where first='$firstName'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
$json = json_encode($output);
echo($json);
mysql_close();
?>
我的PHP代碼,這是我的HTML代碼與jQuery和JSON保存爲index2.html
$("#myForm").submit(function(){
var postData = $(this).serialize();
$.ajax({
type: "GET",
dataType: "json",
data: $("#myForm").serialize(),
url: "db2.php",
success: function(data){
alert("processing now" + data);
$.each(data,function(i,item){
alert("processing each data now");
$('<li></li>').html(item.first + " " + item.last).appendTo('#data');
});
},
error: function(data){alert("failed"+data);}
});
return false;
});
<form id="myForm" method="GET" action="">
<input type="textbox" name="firstName" id="firstName">
<input type="submit" name="myButton" value="submit">
</form>
<ul id="data">
</ul>
我後已經執行了下面的html代碼,它只會提示出錯。 我檢查了幾個小時,並且我看不到任何錯誤代碼被正確執行。 這是從PHP
[{"id":"1","first":"wen","last":"hao","age":"123"}]
我想這是因爲你不聲明'$輸出=陣列();'外的while循環。另外,爲什麼jsonp如果你不需要一個回調函數來傳遞來執行json? – Ohgodwhy
請在將此代碼放入Web服務器之前更改此行:$ q = mysql_query(「SELECT * FROM people where first ='$ firstName'」);閱讀SQL注入和如何防止它 – iamkrillin
[爲什麼$ foo \ [bar \]錯誤?](http://www.php.net/manual/en/language.types.array.php#language.types .array.foo-bar) – deceze