我是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>");
});
但是所欲望的行动正在逐行获得价值: - 第一行值 - &gt;在jquery中采取某些行动; 第二行值来了 - &gt;采取行动..; 。 。 等等。
答案 0 :(得分:0)
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>");
});
答案 1 :(得分:0)
我会让php函数返回一个json响应。您可以通过两种方式执行此操作:通过while语句服务器端手动构造JSON,或使用json_encode
PHP函数并回显服务器端。这样,当您的ajax响应中的客户端返回数据时,您可以将JSON数据解析为JSON对象JSON.parse(json);
,然后以结构化方式逐行控制数据。
希望这有帮助!
答案 2 :(得分:0)
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 p Page.html,身体内部。此选择框将从ajax请求中填充。
<select id="ajaxpopulate"></select>