I have an empty table and the table contains 3 different headings.
I need to get the individual data from a PHP script using jquery.get method and insert each row into the table.
Quite unsure on how to do this. It’s just returning the whole data set when I click on the button.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("file.php",
{name: "name",
address: "address",
type: "type"
},
function(data){
$("#name").append(data);
});
});
});
</script>
</head>
<body>
<h3> Table for Stadiums </h3>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Type</th>
</tr>
<tr>
<td id='name'></td>
<td></td>
<td></td>
</tr>
</table>
<button> Click </button>
</body>
</html>
2
Answers
You have to iterate
data
to form a table, please take a reference from below code for the same, as you can use the samerenderTable
method, just pass your data object to it:Twisty said right: we don’t know what data comes back from the server!
Assuming that file.php returns an array of stadiums your callback function should look something like this: