I’m trying to populate a table structure with around 5 fields of data from mySQL. I’m completely new to web development, so I’m finding this to be rather difficult.
This is the javascript line that I’m currently using to populate my html elements:
$.get("reports.php",
{"param":"getusers"},
function(returned_data)
{
alert(returned_data);
document.getElementById("allusers").innerHTML = returned_data; // Clear the select
});
and this is the php it accesses:
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;
This retrieves the data perfectly, but I need to fit it into a a structure with a few columns and rows. I can’t figure out how to do this. I’ve tried many solutions, some of which involving css, but I can’t figure it out.
I like this example but it only displays the source code when I use it. I don’t know how to call over to my reports.php either, which makes it a bit messy.
How would I go about sending this data to my html file and structuring it to display in an organized format?
I would very much appreciate any help,
Thanks.
You’ve got the basic structure already. Just have your PHP code generate table html instead of options:
then insert that html into your table in Javascript:
As far as ajax/php is concerned, you’re just sending some plain text back and forth. it’s up to your code to decide what to do with that plain text.