get the queryselector result of the id and save it to the database

0

I hope you can help me one more time. The issue is that I must save the results that I get in the database javascript. and the value that the first select of the id="hotels" keeps is the price instead of the hotel .. and I also need to show the price of children and adults in an input to keep them in the db.             Reservation Transport

        <select name="hotel" class="cont form-control" id="hoteles" required> <!--COMBO BOX hoteles-->
             <?php 
             $consulta = "SELECT hotel,precio From hoteles where id = id_zona";
             $query=mysqli_query($conexion,$consulta);
             while ($f=mysqli_fetch_array($query,MYSQLI_ASSOC)){ 
                $id = $f['id'];
                $hotel = $f['hotel'];
                $precio = $f['precio'];
            ?>                      
               <option  value="<?php echo $precio ?>">  <?php  echo $hotel; ?> </option>
            <?php            
                 }         
            ?>        
         </select> 
                 <p></p>

            <span id="" class="">Adults:</span>
            <select name="num_adultos" class="  form-control" id="adultos" required> 
             <?php 
             $consulta = "select * From numero_clientes";
             $query=mysqli_query($conexion,$consulta);
             while ($h=mysqli_fetch_array($query,MYSQLI_ASSOC)){ 
                $num_adult = $h['numero_personas'];

            ?>                      
              <option><?php  echo $num_adult ?> </option>

            <?php           
                 }         
            ?>        
         </select> 
    <p></p>


     <label type="text"   name="pre_adulto" id="pre_adulto"></label>


     <p></p>    
             <span id="tituloCombo">Children:</span>
             <select name="num_ninos" class="contN form-control" id="ninos"> <!--COMBO BOX niños-->
             <?php 

             $consulta = "SELECT * FROM numero_clientes";
             $query=mysqli_query($conexion,$consulta);
             while ($c=mysqli_fetch_array($query,MYSQLI_ASSOC)){
                       $num_nino =$c['numero_personas']; 

            ?>                      
              <option><?php  echo $num_nino; ?> </option>
            <?php 
                 }         
            ?>        
              </select>




    <label class="titulo4" disabled name="pre_ninos"  id="pre_ninos"></label> &nbsp;<label class="titulo4">USD</label>
    <p></p>
    <span class="titulo4"> Price:&nbsp;$</span>
    <label class="titulo4" id="precio-total" disabled></label>

< ------------------------ > script the script is not mine ------------ >

document.querySelectorAll("select").forEach(function(selector) { // funcion de multiplicar pasajeros.
     selector.addEventListener('change', function(e) {
    var price = document.querySelector("#hoteles").value;
    var nb_adults = document.querySelector("#adultos").value;
    var adu = document.querySelector("#pre_adulto  ").innerHTML = price*nb_adults;
    var nb_kids = document.querySelector("#ninos").value;
    var nin = document.querySelector("#pre_ninos").innerHTML= price*nb_kids;
    document.querySelector("#precio-total").innerHTML =  (adu+nin);
    console.log({adu, nin});
  })
});
    
asked by jorge enrique chan castillo 27.12.2017 в 00:40
source

2 answers

0

You must make a request AJAX against your server PHP sending you the data you need and on your server treat this request and return a response. The communication between client and server is normally in the format JSON .

  

An example of AJAX in JavaScript

var req = new XMLHttpRequest();
req.open('POST', '/ruta/del/servidor', true);
var data = {}; // Aquí tus datos a enviar
req.onreadystatechange = function() {
  if (this.readyState === 4) {
    if (this.status >= 200 && this.status < 400) {
      var res = JSON.parse(this.responseText);
      // Código en caso de que haya ido bien...
    } else {
      // Código en caso de que haya ido mal...
    }
  }
};
req.send(data);
    
answered by 27.12.2017 в 00:48
0

First, you can not send the id if you do not insert it into the html in some way. Since a select , by default, sends its value , it is the ideal field for it. As you also want to store the price, but only to calculate from javascript, we will save this in a data field.

The price totals, as I mentioned in a previous question, you should calculate them directly on the server for security, but if you want to send them anyway, they should be in a field of the form, for example input . As the user should not be able to modify them (although, again, it is impossible to avoid it safely, we are talking here only about UX), we will give them the attribute readonly .

In the Javascript, we change where we get the price ( value becomes getAttribute('data-price') ) and where we put the results ( value instead of innerHTML ).

<form method="POST">
        <h1 id="titulo3">Reservation Transport</h1>

        <select name="hotel" class="cont form-control" id="hoteles" required> <!--COMBO BOX hoteles-->
             <?php 
             $consulta = "SELECT hotel,precio From hoteles where id = id_zona";
             $query=mysqli_query($conexion,$consulta);
             while ($f=mysqli_fetch_array($query,MYSQLI_ASSOC)){ 
                $id = $f['id'];
                $hotel = $f['hotel'];
                $precio = $f['precio'];
            ?>                      
               <option  value="<?= $id ?>" data-price="<?php echo $precio ?>">  <?php  echo $hotel; ?> </option>
            <?php            
                 }         
            ?>        
         </select> 
                 <p></p>

            <span id="" class="">Adults:</span>
            <select name="num_adultos" class="  form-control" id="adultos" required> 
             <?php 
             $consulta = "select * From numero_clientes";
             $query=mysqli_query($conexion,$consulta);
             while ($h=mysqli_fetch_array($query,MYSQLI_ASSOC)){ 
                $num_adult = $h['numero_personas'];

            ?>                      
              <option><?php  echo $num_adult ?> </option>

            <?php           
                 }         
            ?>        
         </select> 
    <p></p>


     <input type="text" readonly="readonly" name="pre_adulto" id="pre_adulto">

     <p></p>    
             <span id="tituloCombo">Children:</span>
             <select name="num_ninos" class="contN form-control" id="ninos"> <!--COMBO BOX niños-->
             <?php 

             $consulta = "SELECT * FROM numero_clientes";
             $query=mysqli_query($conexion,$consulta);
             while ($c=mysqli_fetch_array($query,MYSQLI_ASSOC)){
                       $num_nino =$c['numero_personas']; 

            ?>                      
              <option><?php  echo $num_nino; ?> </option>
            <?php 
                 }         
            ?>        
              </select>

    <input class="titulo4" readonly="readonly" name="pre_ninos"  id="pre_ninos"> &nbsp;<label class="titulo4">USD</label>
    <p></p>
    <span class="titulo4"> Price:&nbsp;$</span>
    <input class="titulo4" readonly="readonly" id="precio-total" disabled>

< ------------------------ > script the script is not mine ------------ >

document.querySelectorAll("select").forEach(function(selector) { // funcion de multiplicar pasajeros.
     selector.addEventListener('change', function(e) {
    var price = document.querySelector("#hoteles").getAttribute("data-price");
    var nb_adults = document.querySelector("#adultos").value;
    var adu = document.querySelector("#pre_adulto  ").value = price*nb_adults;
    var nb_kids = document.querySelector("#ninos").value;
    var nin = document.querySelector("#pre_ninos")value = price*nb_kids;
    document.querySelector("#precio-total").value = adu+nin;
    console.log({adu, nin});
  })
});
    
answered by 28.12.2017 в 10:25