How to run php script and update view?

0

I need you to give me an idea of how to solve the following problem ... It is required that when a search is made, the searched product is registered in a database and the time, and it is necessary to show the data of the search in a new form, write in the base and the problem is that I put my script At the beginning of the form where the data is displayed, then when reloading the page again it is reinserted into the database.

                   <form class="" action="detalleProducto.php" method="POST">
                      <div class="input-group">
                        <input type="text" placeholder="Ingrese Nombre del Producto" pattern="\w{5,12}" name="producto" class="form-control" required>
                        <span class="input-group-addon">
                        </span>
                        <span class="input-group-btn">
                          <button class="btn btn-default" type="submit" >
                            <span class="glyphicon glyphicon-search"></span>
                          </button>
                        </span>
                      </div>
                  </form>

is the part where I ask for the name of the product and at the beginning of detalleProduct.php I have this ....

          $user = $_SESSION['usuario'];
          $date = date('Y-m-d H:i:s');
          $h = new Historial();
          $consulta = $h->registrarConsultas(strtoupper($productoBuscado),$date, $user);

and then you have the form where the data is displayed, and therefore when reloading, the record is re-executed, I do not know how to separate it and associate it with the button of the first form.

    
asked by Alberto Rojas 10.12.2016 в 01:14
source

3 answers

0

The simplest thing that occurs to me is that you do it with AJAX

 <form class="" action="detalleProducto.php" id="busqueda" method="POST">
                  <div class="input-group">
                    <input type="text" placeholder="Ingrese Nombre del Producto" pattern="\w{5,12}" name="producto" id=""producto class="form-control" required>
                    <span class="input-group-addon">
                    </span>
                    <span class="input-group-btn">
                      <button class="btn btn-default" type="submit" >
                        <span class="glyphicon glyphicon-search"></span>
                      </button>
                    </span>
                  </div>
              </form>

jQuery:

$("#busqueda").submit(function(){
var data = $("#producto").val();
$.ajax({
    url: "tu_php",
    type: POST,
    data: data,
});  

});

    
answered by 10.12.2016 / 05:56
source
0

using jquery you can send the data to the php script, capture it and save it. Then print the result of the search query with echo and place the data in the new form (The whole process is done in the same script). Ex:

$("#busqueda").submit(function(){
   var valor = $("#producto").val();
   $.post('script.php',{valor:valor}function(data){
       //Pasamos los datos para el nuevo formulario de acuerdo al selector
       //$(selector).html(data.nombre)
   },'json');
   //Ejecutamos return false para evitar que la página se recargue por el submit del formulario.
  return false;
});

Note: the printing of the php script side what you have to do with json_encode in case you do not want to use just have to remove ' json' from the $ .post ()

    
answered by 14.12.2016 в 00:38
0

The problem is that you do the search with the POST method. When reloading the page, the headers are forwarded, including the form, and as in your script, I guess that if POST exists do something, because the condition is met again and the code is executed each time you reload the page.

What I would do according to your code, is to register the searched product and its time in a file different from the form. When the information is saved, redirect to the form with the product data.

This helps you to have more structured (and separate) your code.

It would be something like the following:

form.php as you have it, but in the action:

action="guardar_detalle.php"

Or the name you want to place. This file will save the query.

save_detail.php

if(isset($_POST['producto']))
{
  // saneo xss parte 1
  $producto = strip_tags($_POST['producto']);

  // Incluyo mi conexión con DB, saneo antes de insertar en DB con real_escape_string

 // Preparo mi sql e inserto

// Si hubo error al insertar, guardo la query en una archivo log

// Redirijo a detalleProducto.php con header('location: detalleProducto.php?producto=$producto')
}

// Si no entra al if, entonces redirigir a donde quieras
header('location: index.php');

And now in productoProduct.php I verify that my GET variable exists, which I will capture and re-apply filters.

if(isset($_GET('producto')))
{
  $producto = strip_tags($_GET['producto']);

  // Incluyo mi conexión

  // Hago mi consulta para mostrar los detalles con sus filtros para evitar inyeccción sql

 // Muestro los detalles
}

And you are ready, even if you reload the page, all you will do is to check the DB again according to the GET variable and show the details.

You must take into account that the way you have it is not the best option due to the constant consultations with the DB.

Another option is as the other colleagues say, change your logic using ajax and it is even better for the user experience because if you are looking for a non-existent product you will have to return to the form, or at least it is the impression given with the information that You placed.

Even my answer is not so super detailed for the same, and I think it is unnecessary to place the queries and everything you already know how to do.

    
answered by 14.12.2016 в 16:05