I have a code, in html, i have a button that i want, when user click on this button, PHP query will run to get data from sql data base and also jquery will toggle display of a div that will display data from the database.
`<div class="btn-group" role="group" aria-label="Basic example" style="border:0.3em">
<button type="button" id = 'apartmentrent' name = "apartmentrent" class="btn btn-success">List of apartments ready for rent </button>
</div>
<div id="apartmentrent" style="display:none">
<table border="1">
<tbody id="yourtab">
</tbody>
</table>
</div>
<div><a href="index.php?logout=1" class="logout"> logout</a></div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.js"
<script type="text/javascript">
// Do this *after* the table tag is rendered
$(document).ready()(
$( "#apartmentrent" ).click(function() {
$("#apartmentrent").toggle()
})
</script>
</body>
</html>
PHP CODE
if(isset($_POST['apartmentrent'])) {
$stmt = $pdo->query("SELECT rent_interest.Property_number, rent_interest.Client_name, rent_interest.Phone_number, rent_interest.email, rent_interest.date_inspection,
property.agent_id, property.buildingtype, property.Address, property.floor, property.Rent, property.Agent_fee, property.Barr
FROM rent_interest
LEFT JOIN property ON rent_interest.Property_number = property.Property_number
ORDER BY rent_interest.Property_number");
$rows = array();
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
$rows[] = $row;
}
}`
JSON CODE TO DISPLAY DATA FROM SQL INTO HTML TABLE
$( "#agentsverificationb" ).click(function() {
$("#agentsverification").toggle()
$.getJSON('getjson.php', function(rows) {
$("#mytab").empty();
console.log(rows);
found = false;
for (var i = 0; i < rows.length; i++) {
row = rows[i];
found = true;
window.console && console.log('Row: '+i+' '+row.agent_id);
$("#mytab").append("<tr><td>"+htmlentities(row.agent_id)+'</td><td>'
+ htmlentities(row.Account_number)+'</td><td>'
+ htmlentities(row.Govt_id)+'</td><td>'
+ htmlentities(row.passport)+'</td><td>'
+ htmlentities(row.Bank_name)+"</td><td>\n"
+ '<a href="authController.php?id='+htmlentities(row.user_id)+'">'
+ 'Verified</a>\n</td></tr>');
}
if ( ! found ) {
$("#mytab").append("<tr><td>No entries found</td></tr>\n");
}
}));
When i click the button, my button disappear, my php code doesnt run.
This JSON code doesnt work but JSON successfully got data from SQl and it is holding it as JSON array method.