Pass PHP ID to Jquery Modal Window

Asked

Viewed 625 times

-3

I need to finish an urgent project and there’s something I need to do and I have no idea how to do it. I am doing a student registration system, where I will search by name, Cpf or class time.

In all research methods will be not exact searches, that is to say it will bring me more than one database result. I want, when clicking on the name of the person who appeared, to open a Jquery modal window with the complete data of that person to edit or add one or another. I’m going to apply that to other parts of the system and I need it urgently in order to continue.

I separated and recorded a video showing the problem and presenting a little system, follow link on youtube: https://youtu.be/iWwHJI8Kl0k

Follow my Entire PHP:

    <!-- Modal Avisos -->    
        <div id="boxes">
            <div id="dialog" class="window">
              <div class="title-modals"> <div class="img_warm"> </div> <h3> Aviso </h3></div>
              <div class="cont-modal">
                <?php 
                    if($acao == "search_name") {
                      if(empty($_POST['nome'])) {
                        echo"<h3> Campo Nome Vazio, favor preencha para poder pesquisar!!! </h3>";
                      } else{
                        if($check_name != 1) {
                          echo"<h3> Nome não encontrado, favor verifique e tente novamente! </h3>";
                        }
                      }
                    };
                    if($acao == "search_cpf") {
                      if(empty($_POST['cpf'])) {
                        echo"<h3> Campo CPF Vazio, favor preencha para poder pesquisar!!! </h3>";
                      } else{
                        if($check_cpf != 1) {
                          echo"<h3> CPF não encontrado, favor verifique e tente novamente </h3>";
                        }
                      }
                    };
                    if($acao == "search_time") {
                      if(empty($_POST['select_time'])) {
                        echo"<h3> Campo Horario de Estudo Vazio, favor preencha para poder pesquisar!!! </h3>";
                      } else{
                        if($check_time != 1){
                          echo"<h3> Horario não encontrado no banco de dados, favor entrar em contato com o Administrador do Sistema! </h3>";
                        }
                      }
                    }
                ?>
              </div> <!-- final .cont-modal -->

              <a title="Fechar janela de confirmação" class="close">Entendi</a><br />
            </div>
            <!-- Máscara para cobrir a tela -->
            <div id="mask"></div>
        </div> <!-- final #boxes01 --> 

        <div id="boxes02">
            <div id="dialog02" data-id="<?php $id = $_GET['ID']; ?>" class="window02">
              <?php 
              $result = mysql_query("SELECT * FROM alunos WHERE Dia_Horario LIKE '%".$searched_time."%' " );

              $check_time = mysql_num_rows($result);
                if($check_time >= 1){
                   while($ln = mysql_fetch_array($result)) {
                    $radio = $ln['ID'];
                    $id = $ln['ID'];
                    $full_name = $ln['Nome_completo'];
                    $hora_estudo = $ln['Dia_Horario'];
                }
                }
              ?>
              <form>
                <lable for="txt-nome"> Nome:</lable><input type="txt" id="txt-nome" name="nome" class="txt" value="<?php echo $full_name; ?>" />
              </form>
              <a title="Fechar janela de confirmação" class="close02">X</a><br />
            </div>
            <!-- Máscara para cobrir a tela -->
            <div id="mask02"></div>
        </div> <!-- final #boxes01 --> 


        <div class="result-table">

          <div class="menu-top">
              <a href="" style="text-decoration:none;color:#fff;">
                <div  style="background-color:#06F;" class="bt-top">
                   Alunos 
                </div> <!-- .bt-table -->
              </a>

              <a href="http://www.dpaulatreinamentos.com/system/teste01/views/pages/colaboradores.php" style="text-decoration:none;">
                <div  class="bt-top">
                   Colabores 
                </div> <!-- .bt-table -->
              </a>
          </div> <!-- .menu-table -->

          <div id="form">
            <form id="form_nome" class="forms" method="post" action="?acao=search_name">
              <label for="txt-nome"><h3> Nome   </h3></label> <input type="text" name="nome" class="txt-medium txt" id="txt-nome" /> 
                <div class="tooltip">
                  <input type="submit" value="" class="sb-search-small bradius"/>
                  <span class="tooltiptext">
                    Pesquisar Por Nome
                  </span>
                </div> <!-- final .tooltip -->
            </form>
            <form id="form_cpf" class="forms" method="post" action="?acao=search_cpf">
              <label for="txt-cpf"><h3> CPF:    </h3></label> <input type="text" onkeypress="mascara(this, '###.###.###-##')"  name="cpf" maxlength="14" class="txt-small txt" id="txt-cpf" /> 
                <div class="tooltip">
                  <span class="tooltiptext">
                    Pesquisar Por CPF
                  </span>
                    <input type="submit" value="" class="sb-search-small bradius"/>         
                </div> <!-- final .tooltip -->

              <div class="tooltip">
                <a href=""> <div class="bt-more-small"> </div> </a>
                <span class="tooltiptext">
                  Cadastrar Aluno
                </span>
              </div> <!-- final .tooltip -->



            </form>
            <form id="form_search_class" class="forms" method="post" action="?acao=search_time">
              <lable for="select_time"> <h3> Dia e Horário: </h3>  </lable> <select id="select_time" class="bradius txt-time" name="select_time">
                  <option value="valor1"><?php 
                      $select_time_database = "SELECT * FROM horarios_aulas";
                      $check_time = mysql_query($select_time_database) or die (mysql_error());
                                 while($ln = mysql_fetch_array($check_time)) {
                                  $selected_time = $ln['Horario'];
                                   echo"<option value='$selected_time'> $selected_time </option>";
                                 }
                        ?>
                  </option> 
                </select>
              <div class="tooltip">
                <span class="tooltiptext">
                  Pesquisar Por horarios
                </span>
                <input type="submit" value="" class="sb-search-small bradius"/>
              </div> <!-- final .tooltip -->

              <div class="tooltip">
                <a href=""> <div class="bt-more-small"> </div> </a>
                <span class="tooltiptext">
                    Cadastrar novo horário de aula
                </span>
              </div> <!-- final .tooltip -->

            </form> 

         </div> <!-- final #forms -->

            <table class="com_table">
              <tbody>
                <tr>
                  <th scope="col">  </th>
                  <th scope="col"> ID </th>
                  <th scope="col">Nome </th>
                  <th scope="col">Hora e Dia Estudo</th>
                  <th scope="col">&nbsp;</th>
                  <th scope="col">&nbsp;</th>
                  <th scope="col">&nbsp;</th>
                  <th scope="col">&nbsp;</th>
                  <th scope="col">&nbsp;</th>
                </tr>


                <?php //seleciona toda a tabela

                    if($startaction == 3){
                      if($acao == "search_cpf") {
                        if(empty($_POST['cpf'])) {
                          echo'<script type="text/javascript">abre_modal( \'#dialog\' );</script>';
                        } else {
                        //pegar cpf via post
                        $cpf = $_POST['cpf'];
                        $cpf = preg_replace("/\D+/", "", $cpf);

                       $result = mysql_query("SELECT * FROM alunos WHERE CPF LIKE '%".$cpf."%' " );

                        $check_cpf = mysql_num_rows($result);
                          if($check_cpf >= 1){

                             while($ln = mysql_fetch_array($result)) {
                              $id = $ln['ID'];
                              $full_name = $ln['Nome_completo'];
                              $cargo = $ln['Cargo'];
                    ?>

                        <tr>
                          <a href=""><td> <?php echo $id; ?></td>  </a>
                          <td> <a name="modal01" href="#dialog01" onclick="$('#modal-container').load('<?php echo $id; ?>', function(){$('#dialog01').modal('show');})" > <?php echo $full_name; ?> </a> </td> 
                          <td> <?php echo $cargo; ?></td>
                        </tr>

                    <?php
                      } } else {
                        echo'<script type="text/javascript">abre_modal( \'#dialog\' );</script>';
                      }
                      }
                    }
                    }   

                    if($startaction == 4){
                      if($acao == "search_name") {
                        if(empty($_POST['nome'])) {
                          echo'<script type="text/javascript">abre_modal( \'#dialog\' );</script>';
                        } else {
                        //pegar nome via post
                        $nome = $_POST['nome'];
                        $result = mysql_query("SELECT * FROM alunos WHERE Nome_completo LIKE '%".$nome."%' " );

                        $check_name = mysql_num_rows($result);
                          if($check_name >= 1){

                             while($ln = mysql_fetch_array($result)) {
                              $id = $ln['ID'];
                              $full_name = $ln['Nome_completo'];
                              $hora_estudo = $ln['Dia_Horario'];

                  ?>


                       <tr>
                        <a href=""><td> <?php echo $id; ?></td></a>
                         <a href="#TD"> <td> <a data-id="" href='#dialog01' name="modal01" id="<?php  echo $id; ?>"> <?php echo $full_name; ?> </a> </td> </a>
                         <td> <?php echo $hora_estudo; ?></td>
                      </tr>




                    <?php

                      } } else {
                        echo'<script type="text/javascript">abre_modal( \'#dialog\' );</script>';

                      }
                      }
                    }

                    }

                    ?>

                <?php
                if($startaction == 5){
                      if($acao == "search_time") {
                        if(empty($_POST['select_time'])) {
                          echo'<script type="text/javascript">abre_modal( \'#dialog\' );</script>';
                        } else {
                        //pegar nome via post
                        $searched_time = $_POST['select_time'];
                        $result = mysql_query("SELECT * FROM alunos WHERE Dia_Horario LIKE '%".$searched_time."%' " );

                        $check_time = mysql_num_rows($result);
                          if($check_time >= 1){
                             while($ln = mysql_fetch_array($result)) {
                              $radio = $ln['ID'];
                              $id = $ln['ID'];
                              $full_name = $ln['Nome_completo'];
                              $hora_estudo = $ln['Dia_Horario'];

                  ?>

                    <div class="scroll-y">

                       <tr>
                        <td> <a data-toggle="modal02" data-target="#dialog02" href="#<?php echo $radio; ?>"> Clicar Aqui</a></td>
                        <a href=""><td> <?php echo $id; ?></td></a>
                         <a href="#TD"> <td> <a data-toggle="modal02" data-target="#dialog02"href='#dialog02' name="modal02" id="<?php  echo $id; ?>"> <?php echo $full_name; ?> </a> </td> </a>
                         <td> <?php echo $hora_estudo; ?></td>
                      </tr>

                    </div>


                    <?php

                      } } else {
                        echo'<script type="text/javascript">abre_modal( \'#dialog\' );</script>';

                      }
                      }
                    }

                    }

                    ?>


                </table>
          </tbody>
      </div> <!-- final .result-table -->

        </div> <!-- final .container -->     
  </body>
</html>
<?php 
  • One way to do it is with Javascript and AJAX. When the guy clicks on the edit button, Javascript grabs the ID and makes an AJAX request in a PHP file, this PHP file selects it in the database, bringing the registration data you need. After that, you fill the modal data with Javascript.

  • It is not to edit, the data will already appear in the own modal already with the fields released for editing, after I just do an update in the bank there is already easier, I recorded a video and posted on youtube to help understand my problem.

  • So... when you click on the user name, do an AJAX that takes the data from that record and populates the modal with Javascript.

  • That’s the problem, I don’t know how to do it in javascript, I only know how to do it in PHP

  • Study about AJAX then. What is not lacking is content somewhere from AJAX with PHP and jQuery.

  • There must be some way to do this with Jquery and PHP, Jquery in modal and php in the Back-end

  • Thank you so much for your help

  • The way I spoke is just like that. PHP in the back-end and jQuery in the modal.

Show 3 more comments

2 answers

0

I think you should make the following display all the results

   <?php 
              $result = mysql_query("SELECT * FROM alunos WHERE Dia_Horario LIKE '%".$searched_time."%' " );
$check_time = mysql_num_rows($result);
echo "<ul>";  // adicionado
if($check_time >= 1){
                   while($ln = mysql_fetch_array($result)) {
                   echo "<li>".$ln['Nome_completo']."</li>";   //adicionado   
                }
                }
echo "</ul>"; // adicionado
              ?>              

0

  • Search results PHP: ... echo ("<a href='#' classe='nomecompleto' title='".$full_name"'>".$full_name."</a>"); ... Save the data on the client side this way, so you don’t need to make a second query when you want to edit.

  • jQuery + Localstorage By clicking on the student’s name, it is copied to the localStorage Codejquery: $('document').ready(function (){ $('.nomecompleto').click(function(){ localStorage.setItem('nome', $(this). attr ('title')); }); }); Now the student name has been saved in the Torage localStorage

  • In modal or anywhere else Supposing #editarnome is the id of input for editing the name, automatically loads the student name stored in the localStorage $('#editarnome').val() = localStorage.getItem('nome');

I hope I’ve helped

  • Didn’t work, keeps showing the last record

  • Do you have skype? let’s schedule to chat so you can help me better

  • Skype: agneliox . Unfortunately it can only be tomorrow

  • All right, no problem, I already sent you the request

  • We can arrange a day and time to talk Agnélio?

  • Shows the last record just because you looped it is just used the result at the end of the loop. Lets elaborate better...

Show 1 more comment

Browser other questions tagged

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