我是Ajax的初学者。我想从主题表中获取数据行,仅包含一列主题作为varchar(100),在MySQL DB中定义。以下是我的php代码:
Data.php
<?php
$con=mysqli_connect("","root","root","DBTemp") or die("</br> Error: " .mysqli_connect_error());
$sql="select * from Subject";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo $row["SUBJECT"];
//I Want This Value to Be received in my Jquery Page
//So that i can take certain action based on each Subject.
//For example creating a select box child elements,options.
}
?>
Jquery.js
$(document).ready(function()
{
var response='';
$("body").ready(function()
{
$.ajax(
{
url: '/Data.php',
type: 'GET'
success: function(text)
{
response=text;
}
});
});
$("body").append("<select> /*Get values here as options*/ </select>");
});
但是期望的操作是一行一行地获取值,例如:-第一行值出现->在jquery中采取一定的行动;第二行值来了->采取一定的行动…;..等等。
Data.php
<?php
$con=@mysqli_connect("","root","root","DBTemp");
# Instead of that use header 500 to let javascript side know there is a real error.
if (mysqli_connect_errno())
{
echo "Could not connect to database : ". mysqli_connect_error();
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit();
}
$sql="select * from Subject";
$result = mysqli_query($con,$sql);
if (mysqli_error($con))
{
echo "Query failed : ".mysqli_error($con);
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit();
}
$options = array();
# populate options arrey with using table's id field as key
# and subject field as value.
while($row = mysqli_fetch_assoc($result))
{
$options[$row['id']] = $row['subject'];
}
# return json encoded array to parse from javascript.
echo json_encode($options);
Data.php将输出:
{"1":"Subject ID 1","2":"Subject ID 3"}
Jquery.js
$(document).ready(function()
{
$("body").ready(function()
{
$.ajax(
{
url: '/Data.php',
type: 'GET',
dataType: 'json', // Let jQuery know returned data is json.
success: function(result)
{
$.each(result, function(id, subject) {
# Loop through results and add an option to select box.
$("#ajaxpopulate").append( new Option(subject,id) )
});
}
});
});
});
Page.html,在body内部。这个选择框将从ajax请求中填充。
<select id="ajaxpopulate"></select>
我会让php函数返回json响应。您可以通过两种方式实现这一点:通过while语句在服务器端手动构造JSON,或者使用json_encode
PHP函数并回显该服务器端。这样,当数据在ajax响应中返回客户端时,您可以将JSON数据解析为JSON对象JSON.parse(json);
,然后以结构化的方式逐行控制数据。
希望这对你有帮助!
1)您需要使用像数组这样的数据结构,并将其作为json响应传递给ajax调用。2)你需要迭代你的json数组,这是你可以单独处理每一行,并创建嵌套的选择选项。
更新$con=mysqli_connect("","root","root","DBTemp") or die("</br> Error: "
.mysqli_connect_error());
$sql="select * from Subject";
$result = mysqli_query($con,$sql);
$jsonResult = [];
while($row = mysqli_fetch_assoc($result))
{
$jsonResult[] = $row["SUBJECT"];
}
echo json_encode($jsonResult);
jquery应该是这样的
$(document).ready(function()
{
var response='';
$("body").ready(function()
{
$.ajax(
{
url: '/Data.php',
type: 'GET'
dataType : 'JSON',
success: function(data)
{
//Alert should return an array of your subjects
//If it does then you need to iterate through this array and create options manually.
alert(data);
}
});
});
$("body").append("<select> /*Get values here as options*/ </select>");
});