Fill Modal with Database data

Asked

Viewed 503 times

0

I have a Grid where I click the edit button and it should bring BD data in a Modal but it is not working.

Follow the code.

Knob

Obs. (The Button is bringing the id)

<a href="#my_modal" data-toggle="modal" data-fornecedor-id="<?php echo $rowPedido->id; ?>" >

Div to Return Data

<div class="fetched-data"></div>

Jquery

      $(document).ready(function(){
    $('#myModal').on('show.bs.modal', function (e) {
        var rowid = $(e.relatedTarget).data('fornecedor-id');
        $.ajax({
            type : 'post',
            url : 'readDetails.php', //Here you will fetch records 
            data :  'rowid='+ rowid, //Pass $id
            success : function(data){
            $('.fetched-data').html(data);//Show fetched data from database
            }
        });
     });
}); 

Php (readDetails.php)

 $conn = new PDO('**dados da conexão**'); 


//Include database connection
if($_POST['rowid']) {
   $id = $_POST['rowid']; //escape string
    // Run the Query
    // Fetch Records
    // Echo the data you want to show in modal


             $sql = $conn->prepare( "SELECT * FROM Pedidos WHERE id = $id");
             $sql->execute();
            $result = $sql->fetch(PDO::FETCH_ASSOC);


 }
  • An error is already on the line var rowid = $(e.relatedTarget).data('fornecedor-id'); where the attribute fornecedor-id does not exist, so change fornecedor-id for id only. This is an indication, the others depend on the rest of your code that is not available to better analyze.

  • Opa thanks for the touch actually the provider-id was a test I was doing and forgot to take out I believe the problem is in the readDetails.php returns nothing

  • Put a print_r($result) in the readDetails.php to see what returns.

  • Nothing returns a blank screen

  • When you say that the button is bringing the id is because the attribute is being filled correctly in HTML or because in Javascript it is being correctly read? My question is whether after the line var rowid = .... the value is in the variable rowid.

  • It is because it is being filled in correctly in HTML.

Show 1 more comment

1 answer

-1

Set the ajax to:

$.ajax({
    ...
    data : { rowid : rowid },
    ...
})
  • I just changed but it didn’t work

  • After the if you have in readDetails.php, echo "SELECT * FROM Requests WHERE id = $id", and see if the $id is being reported. If an id does not appear it is because it is not even being sent via ajax.

  • You can also put a print_r($_POST), to see what is being sent by ajax.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.