manipulation of values with a select

1

The issue is that I have a table that is of hotels and with a while it brings the name and the price of the hotel in its last position, the matter is that when the user chooses the hotel automatically I throw the price of the hotel hotel, save that price in one variable and multiply it with another select that only brings me number in people. I leave the code:

 <select name="hoteles" class="form-control" id="cont" required> <!--COMBO 
   BOX hoteles-->
             <?php 
             $consulta = "SELECT hotel,precio FROM 'hoteles_mayas'";
             $query=mysqli_query($conexion,$consulta);
             while ($fila=mysqli_fetch_array($query,MYSQLI_ASSOC)){

                 $precio = $fila['precio'];
                 $hotel =  $fila['hotel']; 
            ?>  

             <option selected value="<?php $precio ?>"><?php  echo  $hotel ?></option>

            <?php    
                 }  
                  mysqli_free_result($query);

            ?>  



         </select>

                 <br>
                  <input id="selector" name=""></input>
                 <input  id="value"></input>

              <br>
                <input type="radio" id="" name="tipo_transfer" value="OW"><label id="titulo4">ONE WAY</label>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                <input type="radio" id="tipo_transfer_rt" name="tipo_transfer" value="RT"><label id="titulo4">ROUND TRIP </label>
                <br>

            <span id="tituloCombo">Adults:</span>
            <select name="hoteles" class="form-control" id="contN"> <!--COMBO BOX hoteles-->
             <?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  
                    $coste = $num_adult * $precio;       
                 }         
            ?>        
         </select> 
     <br>
     <label id="titulo5"> &nbsp;<?php echo '$'. $coste ?></label> &nbsp;<label id="titulo5">USD</label>
     <br>     
             <span id="tituloCombo">Children:</span>
             <select name="ninos" class="form-control" id="contN"> <!--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']; 
                        $id = $c['id'];     
            ?>                      
              <option><?php  echo $num_nino; ?> </option>


            <?php 
                       $num = $num_nino;
                       $precio2 = $num * $precio;
                       $total = $precio2 + $coste;

                 }         
            ?>        
              </select>
                  <br> 
    <label id="titulo5">&nbsp;<?php echo '$'. $precio2 ?></label> &nbsp;<label id="titulo5">USD</label>
                   <br>
                   <br>
    <label id="titulo5"> Total: &nbsp;<?php echo '$'. $total ?></label> &nbsp;<label id="titulo5">USD</label>
    
asked by jorge enrique chan castillo 18.12.2017 в 00:31
source

2 answers

1

I've written a jsfiddle with a possible solution to what you ask. I have replaced your PHP code with a bit of HTML with sample values to be able to use it.

I have a couple of important notes:

  • Security: Do the calculation again on the server when receiving the data. It is very easy to modify the HTML before sending anything, so if you do not verify it, a user could change the price to what he would like.
  • I've left the HTML you use more or less as it was (except for some incorrect tags and repeated ids); but I strongly recommend that you learn some modern html5 structure. You have an abuse of <br> (new line) that probably should be replaced by blocks, either <p> , or <div> , depending on the intention; you have repeated ids, badly closed labels, unnecessary labels with generic ids, etc.

Learning HTML can take time, but it is an essential foundation if you want to dedicate yourself to this.

    
answered by 18.12.2017 в 13:31
0
        <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 ($h=mysqli_fetch_array($query,MYSQLI_ASSOC)){ 
                $id = $h['id'];
                $hotel = $h['hotel'];
                $precio = $h['precio'];
            ?>                      
          <option  value="<?php echo $precio;?>" ><?php  echo $hotel; ?></option>
            <?php            
                 }         
            ?>        
         </select> 
            <p></p>

            <span id="tituloCombo" 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  disabled  name="pre_adulto" id="pre_adulto" ></label> <!--Precio Adultos-->           
     <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> <!--Precio Ninos-->
    <label class="titulo4" id="precio-total" disabled></label>

works well with the script !! but at the moment of keeping the hotel in the db; I saved the value $ price in the hotel's field, and the child price and adult price values were passed to php variables.

    
answered by 28.12.2017 в 21:53