Get value in POST variable of a Modal window

1

How about, asking for your help one more time. I'm making a table where I show clients and each client has a button where they show their personal data, I'm doing it when I click on the button to open a modal window and show that information I do it in the following way:

$('#my_modal').on('show.bs.modal', function(e) {
    var bookId = $(e.relatedTarget).data('book-id');
    $(e.currentTarget).find('input[name="bookId"]').val(bookId);
});
<div class="container">
	<form method="POST" action="#" name="form1" class="">
	<h2>Clientes</h2>

	<table class="table">
		<thead>
     		<tr>
		        <th>Código</th>
		        <th>Nombre</th>
		        <th>RFC</th>
		        <th></th>
      		</tr>
    </thead>
<?php 
	while ($res_c = sqlsrv_fetch_array($res_clientes)) {
?>
    <tbody>
    	<td><?php echo $res_c['CIDCLIENTEPROVEEDOR']; ?></td>
    	<td><?php echo $res_c['CRAZONSOCIAL']; ?></td>
    	<td><?php echo $res_c['CRFC']; ?></td>
    	<td><p><a data-book-id="<?php echo $res_c['CCODIGOCLIENTE']; ?>" href="#my_modal"  data-toggle="modal" class="btn btn-danger">Dirección</p></a></td>
    </tbody>
 <?php
}
?>
	</table>
  </div>
  
  <!-- Ventana Modal-->
       <!-- Modal Clientes -->
<div class="modal" id="my_modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
          <h4 class="modal-title">
            <input type="text" name="bookId" value=""/>
          </h4>
      </div>
      <div class="modal-body">
      <input type="text" id="bookId" name="bookId" value=""  />
<?php 
    $id_cliente = $_POST['bookId'];
    echo $_POST['bookId']; 

    $sql_domicilios = "SELECT * FROM admDomicilios WHERE CIDDIRECCION = ".$id_cliente;
    $res_domicilios = sqlsrv_query($con, $sql_domicilios);
    $f_d = sqlsrv_fetch_array($res_domicilios);

  ?>
        <p>Estado: </p> 
        <p>Colonia: </p> 
        <p>Calle: </p> 

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
      </div>
    </div>
  </div>
</div>
  </form>
  

You already send the client's ID to the input text, now my problem is that when making $ _POST ['bookId']; to pass it to a variable and make conditions tells me that it is not found .. I appreciate your attention and help.

    
asked by Checo 11.04.2017 в 19:17
source

1 answer

1

From what I see in a general way (I think) you have the following on the page when it loads.

What you possibly have

Your index.php (example name):

<html>
   <!-- Tus metas,link y demas de inicio/encabezado -->
   <script>
      <!-- en alguna parte declarado; lo coloque aquí -->
      $('#my_modal').on('show.bs.modal', function(e) {
          var bookId = $(e.relatedTarget).data('book-id');
          $(e.currentTarget).find('input[name="bookId"]').val(bookId);
      });
   </script>
<body>
   <!-- Algo antes del modal que armas -->

   <!-- Inicio estrutura de la modal -->
   <div class="modal" id="my_modal">
      <!-- Aqui lo que tienes antes de la sección PHP -->
      <div class="modal-body">
          <input type="text" id="bookId" name="bookId" value=""  />
          <?php 
              $id_cliente = $_POST['bookId'];
              echo $_POST['bookId']; 
              $sql_domicilios = "SELECT * FROM admDomicilios WHERE CIDDIRECCION = ".$id_cliente;
              $res_domicilios = sqlsrv_query($con, $sql_domicilios);
              $f_d = sqlsrv_fetch_array($res_domicilios);
            ?>
        <p>Estado: </p> 
        <p>Colonia: </p> 
        <p>Calle: </p> 

      </div>
      <!-- Aqui lo que tienes despues de la sección PHP -->
   </div>
   <!-- Fin estrutura de la modal -->

   <!-- Algo despues del modal que armas -->
   <!-- Quizas más código -->
</body>
</html>

Now the explanation goes; if you have it that way when you load that particular page, your error is that of a previous process you are not going via POST that parameter.

What you should do (as I see it)

Being on the page where you show the modal make use of Ajax (I will not delve into it, I assume you have it right); something like:

Your index.php :

<html>
   <!-- Lo necesario anrtes del "body" -->
<body>
   <!-- Lo necesario antes de la estructura para la modal -->

   <!-- Inicio: contenedor para la modal -->
   <div class="modal" id="my_modal">
   <!-- Mira que no coloco nada -->
   </div>
   <!-- Fin:    contenedor para la modal -->

   <!-- Lo necesario despues de la estructura para la modal -->
</body>
</html>

Forming the modal in another PHP armed on the server via Ajax; I will name it (here) paraModal.php :

<form [todo lo que necesites colocarle]>
<div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
          <h4 class="modal-title">
            <input type="text" name="bookId" value=""/>
          </h4>
      </div>
      <div class="modal-body">
      <input type="text" id="bookId" name="bookId" value="<?= $_POST['bookId']; ?>"  />
<?php 
    $id_cliente = $_POST['bookId'];
    echo $_POST['bookId']; 

    $sql_domicilios = "SELECT * FROM admDomicilios WHERE CIDDIRECCION = ".$id_cliente;
    $res_domicilios = sqlsrv_query($con, $sql_domicilios);
    $f_d = sqlsrv_fetch_array($res_domicilios);

  ?>
        <p>Estado: </p> 
        <p>Colonia: </p> 
        <p>Calle: </p> 

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
      </div>
    </div>
</div>
</form>

Then by the time you click to show the modal, invoke the Ajax (be sure to pass and indicate the parameters are via POST) call paraModal.php and your result you insert it in id="my_modal" and conclude with the instruction that shows the modal.

    
answered by 11.04.2017 в 21:49