how to use a form within a modal with php?

1

I have a modal and inside this modal I have a form, the problem is that when I save the form it is not saved in my database, but if instead of a modal I use another php page my form if it works if you save, and I do not change anything in the code, I only change the way you access it.

I pass my code

code to open the modal.it works correctly because you already check if the data is sent to the modal

  $(document).ready(function(){  
      $('.egreso').click(function(){  
           var dni = $(this).attr("id");  
             $.ajax({  
                url:"condicion_egreso.php",  
                method:"post",  
                data:{dni:dni},  
                success:function(data){  
                    $('#employee_detail').html(data);  
                     $('#dataModal').modal("show");  
                }  
           });   




      });  
 });

code within the modal

<?php

   date_default_timezone_set('America/Bogota');
setlocale( LC_TIME, 'spanish' );
   include '../conexionbd.php';
   include('funciones.php');

   $dni = $_POST['dni'];
   $consulta_alta ="SELECT cod_situacion,descripcion from situacion where cod_situacion>1";
   $resultado_consulta = mysqli_query($conexion,$consulta_alta); 

   if (isset($_POST['btnguardar'])) 
  {
    $dni = $_POST['dni'];
    $encargado_egreso = $_POST['responsable'];
    $tipo_alta  = $_POST['tipo_alta'];
    $descripcion = $_POST['descripcion'];
    $fecha = $_POST['fecha'];
    $fecha_actual = date('Y-m-d');

    registrar_alta_paciente($conexion, $dni, $encargado_egreso, $tipo_alta, $descripcion,$fecha,$fecha_actual); 


  }

  ?>

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>ASPATPERU-SISBIO</title>


  </head>
  <body>
  <div class="panel panel-primary">
  <div class="panel-heading text-center">
    <h3>
        CONDICION DE EGRESADO
    </h3>
  </div>
  <div class="panel panel-body">
     <form action="" method="POST">
     <div class="form-group">
     <span>RESPONSABLE DE EGRESO</span>
     <input type="hidden" name="dni" value="<?php echo $dni; ?>">
     <input type="text" class="form-control" name="responsable" value="MEDICO GENERAL" >
     </div>
     <div class="form-group">
     <span>TIPO DE ALTA</span>
     <select id="tipo_alta" name="tipo_alta" class="form-control">
        <?php
             while($fila_alta=$resultado_consulta->fetch_array())
              {
           ?>
        <option value="<?php echo $fila_alta['cod_situacion']; ?>"><?php echo $fila_alta['descripcion'];  ?></option>
 <?php
    }
    ?>
     </select>
     </div>
     <div class="form-group">
     <span>DESCRIPCION</span>
     <input type="text" class="form-control" type="text" id="descripcion" name="descripcion" >
     </div>
     <div class="form-group">
     <span>FECHA</span>
     <input type="date" class="form-control" type="text" id="fecha" name="fecha">
     </div>
     <br>
     <div class="col-md-12 text-center">
                    <div class="form-group" style="margin-top: 15px;">
                      <button type="submit" class="btn btn-success" id="btnguardar" name="btnguardar">Guardar</button>                      
                    </div>
       </div>


     </form>
  </div>    
  </div>  
  </body>
  </html>

code to save the form (register_alta_paciente) this function is in a php file called funciones.php if you can see what I called at the beginning

  function registrar_alta_paciente($conexion, $dni, $encargado_egreso, $tipo_alta, $descripcion,$fecha,$fecha_actual)
  {


          $insertar_alta = "INSERT INTO alta_paciente(dni,responsable_alta,tipo_de_alta,fecha,descripcion,fecha_movimiento) VALUES ('$dni','$encargado_egreso','$tipo_alta','$fecha','$descripcion','$fecha_actual')";
          echo $insertar_alta;
          $resultado_insertar = mysqli_query($conexion,$insertar_alta) or mysqli_error($conexion);




  }
    
asked by ingswsm 17.07.2017 в 07:53
source

2 answers

1

Muriano is right, including within the modal the check if (isset ($ _ POST ['btnguar'])) and the call to the function register_alta_paciente, this check and call is not made when the POST is made from the modal. You must take it out of it. This is a very simple example of how the modal call should work and how the response should be collected from outside.

The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<?php

if (isset($_POST["mail"]) ) {
    $mail=$_POST["mail"];
    $insertar_alta = "INSERT INTO mails(id,mail) VALUES (1,'$mail')";
    echo $insertar_alta;
    $resultado_insertar = mysqli_query($conexion,$insertar_alta) or 
    mysqli_error($conexion); 
 }


?>

<div class="container">
<h2>Ejemplo Modal</h2>

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">

  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Modal Header</h4>
    </div>
    <div class="modal-body">
    <form name="new user" method="post" action="index.php"> 
        <input type="text" name="mail"/> <br />
        <input type="submit"  value="grabar"/>
    </form>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>

</div>
</div>

</div>

</body>
</html>
    
answered by 17.07.2017 в 20:14
0

Note that you are calling the registrar_alta_paciente function within the code that generates the form. But the form is loaded in a modal by ajax and this code is not present when the form is received by post.

The code:

<?php

date_default_timezone_set('America/Bogota');
setlocale( LC_TIME, 'spanish' );
include '../conexionbd.php';
include('funciones.php');

$dni = $_POST['dni'];
$consulta_alta ="SELECT cod_situacion,descripcion from situacion where cod_situacion>1";
$resultado_consulta = mysqli_query($conexion,$consulta_alta); 

if (isset($_POST['btnguardar'])) {
    $dni = $_POST['dni'];
    $encargado_egreso = $_POST['responsable'];
    $tipo_alta  = $_POST['tipo_alta'];
    $descripcion = $_POST['descripcion'];
    $fecha = $_POST['fecha'];
    $fecha_actual = date('Y-m-d');

    registrar_alta_paciente($conexion, $dni, $encargado_egreso, $tipo_alta, $descripcion,$fecha,$fecha_actual); 


  }

?>

It should be in the script from which the modal is called, or in the script that appears in the action of your form. As in your case you omit this value, the form is launched against the currently loaded script (I repeat, not against the script that contains the form)

    
answered by 17.07.2017 в 09:32