Input shows the result on three selects

0

Good morning I'm doing the insertion of orders and I need some help. The idea is that according to two selects the result has to come out in an input.

On the one hand we have the producto that is in the table productos

+----+----------+--------+
| id | producto | estado |
+----+----------+--------+
|    |          |        |
|    |          |        |
+----+----------+--------+

The product selection

<select id="select2-2" value="" class="form-control" name="select2-2">
   <option value="0">Seleciona un producto</option>
   <?php
     $resultisa = $mysqli->query("SELECT * 
     FROM productos WHERE estado = 1");                                                   
     mysqli_set_charset("utf8");
     while($resia = $resultisa->fetch_array()) {
  ?>
    <option value="<?php echo $resia['id'] ?>"><?php echo $resia['producto'] ?> <?php echo $resia['alias'] ?></option>

  <?php } ?>
 </select>

On the other hand we have the tipo de contrato that is in the table tipos

+----+----------+--------+
| id |   tipo   | estado |
+----+----------+--------+
|    |          |        |
|    |          |        |
+----+----------+--------+

The selection of contract types

<select id="select2-4" value="" class="form-control" name="select2-4">
   <option value="0">Tipo contrato</option>
   <?php
     $resultisa = $mysqli->query("SELECT * 
      FROM tipos WHERE estado = 1");
      mysqli_set_charset("utf8");
      while($resia = $resultisa->fetch_array()) {
   ?>
     <option value="<?php echo $resia['id'] ?>"><?php echo $resia['tipo'] ?></option>
   <?php } ?>
 </select>

Then we have the table, that depending on what you choose, you have to leave the input in% input Tabla posibles :

+----+----------+--------+---------+-------------+---------+
| id |idproducto|  tipo  | entrada | mensualidad |  final  |
+----+----------+--------+---------+-------------+---------+
|    |          |        |         |             |         |
|    |          |        |         |             |         |
+----+----------+--------+---------+-------------+---------+

Here the Select for the possible table:

<select id="select2-4" value="" class="form-control" name="select2-4">
    <option value="0">Selecciona</option>
    <option value="1">Entrada</option>
    <option value="2">Mensualidad</option>
    <option value="3">Cuota Final</option>
</select>

And here the input where to show the result:

<input type="text" class="form-control inputmiocont corpiii" id="eTotal" name="eTotal">

The idea that if they choose:

First select the id:1 that is Láser de diodo
Second Select: id:2 which is comprar
Third Select: Value="1" that is Entry

In the input they have to leave: 13225
What you have to do is compare why sometimes the field will be empty since that possibility does not exist.

----------------- POSSIBILITIES ---------------------

Table productos

+----+-----------+--------+
| id | producto  | estado |
+----+-----------+--------+
| 1  |LASER DIODO|   1    |    
| 2  | DUALWAVE  |   1    |
+----+-----------+--------+

Table tipos

+----+------------+--------+
| id |   tipo     | estado |
+----+------------+--------+
|  1 | ALQUILER   |    1   |
|  2 | COMPRAR    |    1   |
|  3 | EXPLOTACION|    1   |
|  4 | ALQ.COMPRA |    1   |
+----+------------+--------+

Tabla posibles : I put it in two types of products, but there are more

+----+----------+--------+---------+-------------+---------+
| id |idproducto|  tipo  | entrada | mensualidad |  final  |
+----+----------+--------+---------+-------------+---------+
|  1 |     2    |   1    |    0    |     455     |    0    |
|  2 |     2    |   2    |  13225  |      0      |    0    |
|  3 |     2    |   3    |    0    |      0      |    0    |
|  4 |     2    |   5    |   1900  |     385     |   500   |
|  5 |     1    |   1    |    0    |     435     |    0    |
|  6 |     1    |   2    |  12900  |      0      |    0    |
+----+----------+--------+---------+-------------+---------+

The third selection let's say it's for the ultimate filter, if it's input, menuality or final.

Let's make an example;

We chose the DUALWAVE product from the first selection
We chose from the second select RENT
And we chose in the third selection MENSUALIDAD

Result = 455.
If we had chosen in the third select, input will be = 0

I do not know if this is something clearer, I hope so, thank you!

EDITIONS

Valor del id del producto

<script>                                                          
  $(document).ready(function(){
  $("#select2-2").click(function(){
  var valor= $("#select2-2").val();                                                     
    });
  });
</script>

Valor tipo contrato

 <script>                                                          
   $(document).ready(function(){
   $("#select2-4").click(function(){
   var valor= $("#select2-4").val();                                                     
     });
  });
 </script>

Valor filtro

 <script>                                                          
   $(document).ready(function(){
   $("#select2-3").click(function(){
   var valor= $("#select2-3").val();                                                     
     });
  });
 </script>

Sacar variables

 <script>
   var idProducto = $('#select2-2').val();
   var idTipo = $('#select2-4').val();
   var idFiltro = $('#select2-3').val();
 </script>

Query

<?php $resultado = $mysqli->query("SELECT " . idFiltro . "FROM posibles WHERE idproducto = " . idProducto . " AND tipo = " .idTipo); ?>

imprimimos query

<input type="text" class="form-control inputmiocont corpiii" id="eTotal" name="eTotal" value="<?php echo $resultado;?>">
    
asked by Miguel 13.09.2018 в 08:52
source

2 answers

0

I understand that what you want is to filter, from the IDs selected, the results of a table. If so, it is important to keep in mind that a select does not do anything by itself, it is just a list. We must provide it with logic to perform the actions we want.

To begin with, each select has an "onchange" event that can be captured and from which the value (an ID, in this case) of the selected element can be obtained. It can be done with jQuery or with Javascript by accessing the DOM.

Once the necessary IDs have been obtained, you have to make a query to cross them with the final table and it will return the result.

In your example:

  • Product ID: 2
  • ID Type: 1
  • Filter ID: 2 (this should be defined in a table and use ID's in table posibles )
  

At this point, it is clear that the filter does not have the logic that   you are pretending I do not know if I explain myself well, but you're using   the names of the columns as options in a select , and at the rate of   SQL the query would be quite complicated.

Having the structure as you have it mounted, I would change the values of the options of the last select to be texts with the names of the columns that you want, that is:

<select id="select2-4" value="" class="form-control" name="select2-4">
    <option value="">Selecciona</option>
    <option value="entrada">Entrada</option>
    <option value="mensualidad">Mensualidad</option>
    <option value="final">Cuota Final</option>
</select>

Then, following the previous example, we would have:

  • Product ID: 2
  • ID Type: 1
  • Filter ID: "monthly payment"

And we could already do a query to get the desired value:

SELECT $id_filtro
FROM posibles
WHERE idproducto = $id_producto AND tipo = $id_tipo

And update the value of input with the result of that query.

  

NOTE : I do not consider this the best option since the data structure does not seem correct to me. I would change the tables so I do not have to use chains where ID's should go. But, as you have it, this is an option.

    
answered by 13.09.2018 в 11:13
0

I put it as a separate answer, since the question itself changed.

To begin with, you have several things wrong:

<script>                                                          
  $(document).ready(function(){
  $("#select2-2").click(function(){
  var valor= $("#select2-2").val();                                                     
    });
  });
</script>

These scripts do nothing, they only save a value in a variable that dies when the function ends.

<?php $resultado = $mysqli->query("SELECT " . idFiltro . "FROM posibles WHERE idproducto = " . idProducto . " AND tipo = " .1); ?>

Check that query because you're putting a '1' on fire, and because you do not have the concatenation well in PHP.

<input type="text" class="form-control inputmiocont corpiii" id="eTotal" name="eTotal" value="<?php echo $resultado;?>">

This does not work because $resultado has no value when you start the page and do not update it correctly. After making the execution of the query you should update the input via jQuery, if you do not want to reload the page (which is the only way PHP could update HTML).

I do not know very well how you have all this mounted, so it is difficult for me to explain the steps to follow, but it would be something like this:

<?php $resultado = $mysqli->query("SELECT "... ?>

$('#eTotal').val(<?php $resultado ?>);
    
answered by 17.09.2018 в 09:16