Inserting data in Modal Bootstrap with PHP OO

0

I'm developing a client signup screen with PHP OO but due to my inexperience I'm finding it difficult to list client data within a Modal Bootstrap.

First, I have this list of Client Objects where I present only some basic data of the registered clients: Code:

<?phpforeach($cliente->buscarClientes()as$key=>$value){echo"<tr>
                    <td><input type='checkbox' value='$key'</td>
                    <td>".$value->getNome()."</td>
                    <td>".$value->getCpf()."</td>
                    <td>".$value->getEmail()."</td>
                    <td>".$value->getDataCadastro()."</td>
                    <td>
                        <a href='#' data-toggle='modal' data-target='#modalCliente' data-clientmodal='$key'><i class='fa fa-edit' title='Editar'> </i></a>
                        <a href='#'> <i class='fa fa-trash' title='Excluir'> </i> </a>
                    </td>
                </tr>";
              }
            ?>

Note: LookupClients () returns an Array of Client objects where the index is the client ID.

Modal:

Jquery code of Bootstrap modal:

   <script>
        $(function () {

          // Modal editar/inserir cliente
          $('#modalCliente').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget) // Button that triggered the modal
            var recipient = button.data('clientmodal') // Extract info from data-* attributes

            var modal = $(this)
            if(recipient !== null){ 
              modal.find('.modal-title').text('Editar Cliente (#' + recipient + ")")
              modal.find('.modal-body input#modal-idml').val("example")
            } else {

               modal.find('.modal-title').text('Novo cliente')
            } 

          })

        });
  </script>

I would like the user to press the pencil icon to open Modal with all the data of the respective client to be able to edit them, if possible reusing the object that is already created. I tried to use AJAX in some ways, but I did not understand how to use it for that purpose.

Could you help me and if possible, how do I do this?

    
asked by anonymous 06.08.2016 / 01:42

1 answer

1

A hint of how I would do if I understood I could do it my way, I would create a new folder with a php file - > "data.php" and clicking the button to open the modal.

var acess;
if(window.XMLHttpRequest) {
acess = new XMLHttpRequest();

}

else if(window.ActiveXObject) {
acess = new ActiveXObject("Microsoft.XMLHTTP");
}


var url = "pasta/dados.php?userID="+ID;
acess.open("GET", url, true);
acess.onreadystatechange = function() {
if(acess.readyState == 1) {
document.getElementById('modal').innerHTML = 'Carregando ...';
}

if(acess.readyState == 4 && acess.status == 200) {

var answer = acess.responseText;

document.getElementById('modal').innerHTML = answer;

}
}
acess.send(null);

so in the data.php you put the user data, receive the ID query and return the data, and this page will be processed inside the element with ID "modal" or the ID you have set for your modal

//DADOS.PHP
<?php
$userID = $_GET['userID']??"";
if($userID != ""){
$cliente = $con->query("SELECT Username,Cpf,Email,Data FROM TABELA User WHERE UserID = $userID")


foreach ($cliente->buscarClientes() as $key => $value){
            echo "<tr>
                <td><input type='checkbox' value='$key'</td>
                <td>".$value->getNome()."</td>
                <td>".$value->getCpf()."</td>
                <td>".$value->getEmail()."</td>
                <td>".$value->getDataCadastro()."</td>
                <td>
                    <a href='#' data-toggle='modal' data-target='#modalCliente' data-clientmodal='$key'><i class='fa fa-edit' title='Editar'> </i></a>
                    <a href='#'> <i class='fa fa-trash' title='Excluir'> </i> </a>
                </td>
            </tr>";
          }
}
?>
    
07.08.2016 / 14:37