AJAX getting data from selected two dropdown and giving result PHP

I need to get two data from my both dropdowns. First dropdown gets fabricID and the second gets sizeID.
With these two information, i want ajax to search my database table where it has the fabricID and size selecte so it can give my a price.

I used two functions to get each dropdown data inside of one $(document).ready(function(){...

$(document).ready(function(){

    $('.fabric').on('change',function(){
        var fabricID = $(this).val();
        console.log("fabric id_price is " + fabricID); //debugging
        if(fabricID){
           $.ajax({
                type:'POST',
                url:'calculates.php',
                dataType: 'json',
                data: {
                fabric_id: fabricID
                },

                success:function(html){                                                          
                    //closing tags 
                    //dont need nothing here because i need 
                    //just to get the value from both together

    });

    $('.size').on('change',function(){
        var sizeID = $(this).val();
        if(sizeID){
            $.ajax({
                type:'POST',
                url:'calculates.php',
                dataType: 'json',
                data: {
                size_id: sizeID
                },

                success:function(html){
                    $('.icms').html(html); // i need to get result here 
                    based on the result from calculates.php
               //closing tags

Is this right? Did I build the jQuery script the right way?

Because its not working. Look to my tables: table:almofads is where is stored my fabrics names

table image ht tp://i.stack.imgur.com/TljmS.png

and i will get the id_price from the dropdown fabrics and send the id_price to table:valores_almofadas where price_id has to be equal to the first table.

table image2 ht tp://i.stack.imgur.com/IRA6z.png

So when I select fabricID and sizeID, I will get the result from here…calculates.php

<?php header('Content-Type: application/json');
 include_once '../incluedes/conn_cms.php'; //session started here

 if(isset($_GET["size_id"],$_GET["id_price"])){
    $size_id=$_GET["size_id"] ;
    $id_price=$_GET["id_price"] ;


 $query3 =" SELECT * FROM valores_almofadas 
            WHERE size='$size_id' 
            AND price_id ='$id_price'";

   $result = mysqli_query($conn,$query3);
   while($rows = mysqli_fetch_assoc($result)){

        if($_SESSION['estado'] == 'SP'){
           $ICMS = $rows['icms_7'];
           }else{
           $ICMS = $rows['icms_12'];
           }
           $_SESSION['icms']=$ICMS;

        } echo json_encode($_SESSION['icms']);

} 
?>

so the result will be displayed here in cart.php in $_SESSION[ā€˜icms’]

this is my cart.php

function cart(){
    global $conn;

    $fabric_options = '<option>Select fabric</option>';
    $query2 = "SELECT * FROM almofadas";
    $result = mysqli_query($conn,$query2);
    while($rows = mysqli_fetch_assoc($result)){
     $tecido=$rows['tecido'];
     $id_price=$rows['id_price'];
     //sizes for size dropdown
     $t50='50';
     $t45='45';
     $fabric_options .= '<option value="'.$id_price.'">'.$tecido.'</option>';
    }

 foreach ($_SESSION as $name => $value) {
     if($value > 0){
     if(substr($name, 0, 8 ) == "product_"){
     $length = strlen($name) -8;
     $item_id = substr($name,8 , $length);
     $query = "SELECT * 
               FROM gallery2 
               WHERE gallery2.id =".escape_string($item_id). "";
               $run_item = mysqli_query($conn,$query);
  while($rows = mysqli_fetch_assoc($run_item)){ 
     $vari   = $rows['variante'];
     $num    = $rows['title'];
     $id     = $rows['id'];

 $btn_add ='<button type="button" class="btn btn-success actions plus" data-action="plus" product_id="'.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></button>';

 $btn_remove ='<button type="button" class="btn btn-warning actions less" data-action="remove" product_id="'.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></button>';

 $btn_delete ='<button type="button" class="btn btn-default actions" data-action="delete" product_id="'.$id.'" onclick="deleteRow(this)"><i class="fa fa-times fa-lg" aria-hidden="true"></i></button>';
    if($rows['variante'] < 1){
    $vari="";
    }else{
    $vari = "-".$rows['variante'];
    }

    $product = '
    <tr>
       <td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
       <td>'.$num.''.$vari.'</td>
       <td style="width:15%;">
          <select name="fabric" class="fabric select form-control selectpicker" required=""  >
        '. $fabric_options . '  
          </select>
       </td>
       <td>
          <select  id="" class="select size form-control selectpicker" required style="width:80%;"  >
          <option value="'.$t50.'">50x'.$t50.'</option>
          <option value="'.$t45.'">45x'.$t45.'</option>
          </select>
       </td>
       <td class="product'.$id.'">'.$value.'</td>//quantity of the product
       <td class="icms'.$id.'">R$:'.$_SESSION['icms'] .'</td> // cost of product
       <td class="total'.$id.'">'.$value * $_SESSION['icms'] .' </td> total of product (sub.total)
       <td> 
       '.$btn_add.' '.$btn_remove.' '.$btn_delete.'//buttons
       </td>
     </tr>';
           echo $product;  

        } 
      } 
    }
  }   
}

as you see all my products are in seassions, so each product session must get the value returned ,so each row in my cart has his how cost.
look my UI in checkout.php


So how i will combine two dropdowns to get for each product it cost.
obs: each cost must be in a variable or session.
in case for more code info look here
Please why im not getting the data back?

As I read it, and I’m learning about Ajax, it seems the problem is here:

if(isset($_GET["size_id"],$_GET["id_price"])){

Because your Ajax calls are for each individual drop-down, they each only pass one parameter to the PHP code, hence it never runs because your isset() call needs them both to exist. Is it possible for each Ajax call to get its own value, but also get the value for the ā€œotherā€ drop-down?

var size_ID = $(.size).val();

in the fabric code, and then add that to the dataset? Or isn’t that possible?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.