How to make a select with mysql and php from a modal?

2

I have a modal that opens from a button which comes with the id of the post. This id stores it in an input type "hidden", in the modal.

My problem is having this field already loaded with the selected post id How can I filter data from a database using the value of that field?

this is the input:

<input type="hidden" id="obtenerIdPost"  name="idC">

PHP

As you can see, the query is not complete because I need to load the value of the field into a variable, to do the filtering.

<?php

$query_buscar_posts = mysqli_query($conn, " SELECT * FROM comentarios WHERE id_post = ") or die('Error: ' . mysqli_error($conn));

  while ($posts_filas = mysqli_fetch_array($query_buscar_posts)) {

          $contenido2  = $posts_filas['contenido'];

       echo ' <p class="aaaaa"> '.$posts_filas['contenido'].'</p>';



        }

        ?>
    
asked by luis 20.11.2016 в 17:01
source

1 answer

3

And here is my solution:

HTML

<input type="text" id="obtenerIdPost" name="idC">

JAVASCRIPT

<script type="text/javascript">

$(document).ready(function(){

   $(document).on("click", ".comentarPosts", function () {

       document.getElementById("cajacoments").style.display = 'block';
       var Id = $(this).data('id');
       $('#obtenerIdPost').val( Id );   
       alert(Id);
       var datos = 'idPost='+ Id ;

       $.ajax({
           type:'POST',
           url:'comentarios.php',
           data:datos,
           success:function(result){

               $("#div1").html(result);
           }
      });
   });
});
</script>

PHP     

include('conexion.php');

$id_post = $_POST['idPost'];

$query_buscar_posts = mysqli_query($conn, " SELECT * 
                                            FROM comentarios 
                                            WHEREid_post = $id_post  ") or die('Error: ' . mysqli_error($conn));

while ($posts_filas = mysqli_fetch_array($query_buscar_posts)) {

    // code...
    $contenido2  = $posts_filas['contenido'];         
    echo ' <p class="aaaaa"> '.$posts_filas['contenido'].'</p>';
} 
?>
    
answered by 20.11.2016 / 19:33
source