0

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.

Stephen
  • 15
  • 6
  • hi stephen, welcome to stackoverflow. i have no idea where your supposedly button that sends data to php. but i see `$("#apartmentrent").toggle()` which hides something when clicked. also, a duplicate id `apartmentrent` on button and a div which jquery will take the first one as default - duplicate id should be avoided. – Bagus Tesa Jun 11 '22 at 10:50
  • Thanks for your answer. For php, example, when writing php code, you will always start with If($_POST['submit']){.....} in which most cases, submit is a button in a form. In this case, i dont have a form, i just have a button apartmentrent which i want when it is clicked, a php function will run. but this code dont run – Stephen Jun 11 '22 at 11:14
  • php is a server side (runs on the server) code while javascript (in this case) is a client side one (runs on the browser). triggering the `if($_POST['submit']){...}` will require you to send the data from the client side to the server somehow. you could just print the entire table before hand then toggle it using javascript, post the `$_POST['submit']` thing, or use [ajax](https://www.w3schools.com/xml/ajax_php.asp). – Bagus Tesa Jun 11 '22 at 12:00
  • Thanks, I wrote JSON code to get data from SQL, which was successful. I wrote JQuery code to display this data in tables inside my html page, this didnt work. Included is my JSON code to display data into tables. Thats why i decided to use the button #apartmentrent to send POST to php to run the query and get data from sql and display direct instead of using JSON. is anything wrong with this JSON code? – Stephen Jun 11 '22 at 12:19
  • is that `htmlentities` a php function? i dont remember javascript has such built in function. you could use a few pointer from [this qa](https://stackoverflow.com/a/25207). – Bagus Tesa Jun 12 '22 at 03:54

0 Answers0