Home » Javascript » How do I pass MySQL rows to a specific div in html using PHP?

How do I pass MySQL rows to a specific div in html using PHP?

Posted by: admin August 19, 2018 Leave a comment

Questions:

Good day to everyone. So I am trying to get the value of a select option and getting a set of rows in MySql depending on the column name in the database. So here is the HTML code:

<html>
<body>
<select name = "FilterDoc" onchange = "filterby(this);">
      <option disabled>Filter By</option>
      <option value="document_type">Document Type</option>
      <option value="date">Date</option>
      <option value="hei">HEI</option>
      <option value="other">Other Govt.</option>
      <option value="person">Person</option>
</select>
<div class="panel-body" id="container">
</div>

Here is code for Ajax:

<script type="text/javascript">
function filterby(sel){

  $.ajax({    //create an ajax request to display.php
    type: "POST",
data: {FilterDoc: $(sel).val()},
    url: "filterdocu.php",             
    dataType: "html",   //expect html to be returned                
    success: function(response)
{                    
        $("#responsecontainer").html(response); 
    }
    console.log(reply);
});} </script>

Now the value of the select option will pass to the PHP file. I dont know if “if statement” is the right one for this since I haven’t had that much background about getting values on html and such and I’m trying to find a better way to get the rows from MySql and display them into the container.

Here’s the PHP code:

<?php echo"<div class='panel panel-primary' id='container'>";
        if($_POST["FilterDoc"]=="document_type")
        {
        echo "<script type='text/javascript'>$('container').html('""');</script>";
        $result=mysqli_query($conn,"SELECT * FROM records ORDER BY document_type ASC");

        while($data = json_encode(mysql_fetch_assoc($result))
        {   
            echo json_encode($data);
        }
        }

        else if($_POST["FilterDoc"]=="date")
        {

        $result=mysqli_query($conn,"SELECT * FROM records ORDER BY date_received DESC");
        echo "<script type='text/javascript'>$('container').html('""');</script>";
        while($data = json_encode(mysql_fetch_assoc($result))
        {   
            echo json_encode($data);
        }
        }

        else if($_POST["FilterDoc"]=="hei")
        {

        $result=mysqli_query($conn,"SELECT * FROM records ORDER BY hei ASC");
        echo "<script type='text/javascript'>$('container').html('""');</script>";
        while($data = json_encode(mysql_fetch_assoc($result))
            ($result))
        {   
            echo json_encode($data);
        }
        }

        else if($_POST["FilterDoc"]=="Other")
        {

        $result=mysqli_query($conn,"SELECT * FROM records ORDER BY other_govt ASC");
        echo "<script type='text/javascript'>$('container').html('""');</script>";
        while($data = json_encode(mysql_fetch_assoc($result))
        {   
            echo json_encode($data);
        }
        }

        else if($_POST["FilterDoc"]=="Person")
        {
        $result=mysqli_query($conn,"SELECT * FROM records ORDER BY contact_person ASC");
        echo "<script type='text/javascript'>$('container').html('""');</script>";
        while($data = json_encode(mysql_fetch_assoc($result))
        {   
            echo json_encode($data);
        }
        }echo"</div>"; ?>

I’m also not entirely sure if using javascript to clear the container is the proper way before putting contents into the div container.
I would really appreciate your help. Thank you!

Answers: