【发布时间】:2012-05-29 14:06:33
【问题描述】:
我想在按下 Submit 按钮后从 PHP 脚本中生成的 SQL 语句返回 JSON 数据,但我却收到了 null。
我将使用返回的 JSON 在我的 Google 地图上过滤显示标记,但现在我只想从 PHP 脚本将数据返回到我的 jQuery 页面,以便我可以操作/使用它.
提交按钮:
HTML
<input type="submit" id="filter" value="Filter" />
JS
$('#myform').on('submit', function(e) {
e.preventDefault();
var myData = $('#myform').serializeArray();
$.getJSON('myscript.php', myData, function(json){
alert(json);// actually filter for later
});
});
PHP 脚本:
// action is a hidden form control I use to check if form was submitted
if(isset($_POST["action"])){
if(isset($_POST["color"]) && isset($_POST["zipcode"])){
// try to open a connection to a MySQL server
$connection = mysql_connect($host, $username, $password) or die("Could not connect" . mysql_error());
// select the active MySQL database to work with
$db_selected = mysql_select_db($database, $connection) or die("Can\'t use db:" . mysql_error());
$query = 'sql statement to return resutls based on what color and zipcode was provided';
$result = mysql_query($query) or die("Can\'t do that: " . mysql_error());
}
// close connection to the database
echo json_encode($result);
mysql_close($connection);
}
【问题讨论】:
标签: php javascript jquery sql ajax