用php或jquery填充html表(或类似结构)
我试图用mySQL中大约5个字段的数据填充一个表结构。我对网络开发完全陌生,所以我发现这相当困难 这是我当前用于填充html元素的javascript行:用php或jquery填充html表(或类似结构),php,jquery,html,Php,Jquery,Html,我试图用mySQL中大约5个字段的数据填充一个表结构。我对网络开发完全陌生,所以我发现这相当困难 这是我当前用于填充html元素的javascript行: $.get("reports.php", {"param":"getusers"}, function(returned_data) { alert(returned_data); document.getElementById("allusers").innerHTML = returned_data
$.get("reports.php",
{"param":"getusers"},
function(returned_data)
{
alert(returned_data);
document.getElementById("allusers").innerHTML = returned_data; // Clear the select
});
这是它访问的php:
case "getusers":
$result = mysql_query(
"SELECT * FROM users")
or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$str .= "<option>". $row['username'] .</option>"; //accumulate table
}
echo $str;
break;
这可以完美地检索数据,但我需要将其放入一个包含一些列和行的结构中。我不知道该怎么做。我已经尝试了很多解决方案,其中一些涉及css,但我想不出来
我喜欢,但它只显示源代码时,我使用它。我也不知道如何调用我的reports.php,这使得它有点混乱
我将如何将这些数据发送到我的html文件,并将其结构化以有组织的格式显示
我非常感谢您的帮助
谢谢。您已经掌握了基本结构。只需让PHP代码生成表格html,而不是选项:
$str .= '<tr><td>' . $row['field1'] . </td><td>' . $row['field2'] etc....
就ajax/php而言,您只是来回发送一些纯文本。由您的代码决定如何处理该纯文本。下面的代码将用jquery填充html表
<table id="tablefriendsname">
<tbody>
</tbody>
</table>
$.ajax({
type: "POST",
url: "/users/index/friendsnamefromids",
data: "IDS="+requests,
dataType: "json",
success: function(response){
var name = response;
//Important code starts here to populate table
var yourTableHTML = "";
jQuery.each(name, function(i,data) {
$("#tablefriendsname").append("<tr><td>" + data + "</td></tr>");
});
}
});
<table id="tablefriendsname">
<tbody>
</tbody>
</table>
$.ajax({
type: "POST",
url: "/users/index/friendsnamefromids",
data: "IDS="+requests,
dataType: "json",
success: function(response){
var name = response;
//Important code starts here to populate table
var yourTableHTML = "";
jQuery.each(name, function(i,data) {
$("#tablefriendsname").append("<tr><td>" + data + "</td></tr>");
});
}
});