繁体   English   中英

AJAX从选定的下拉列表中获取数据并给出结果PHP

[英]AJAX getting data from selected dropdown and giving result PHP

在此处输入图片说明

我需要从两个下拉列表中获取两个数据。 第一个下拉列表获取fabricID,第二个下拉列表获取sizeID。 然后,ajax将选定的数据发送到calculates.php

我使用了两个函数来将每个下拉数据存储在一个$(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

这是正确的吗? 我是否以正确的方式构建了jQuery脚本?

因为它不起作用。 查看我的表:table:almofads是存储我的结构名称的位置 在此处输入图片说明

我将从下拉列表中获取id_price并将id_price发送到table:valores_almofadas,其中price_id必须等于第一个表。

在此处输入图片说明

所以当我选择fabricID和sizeID时,我将从这里得到结果... 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']);

} 
?>

因此结果将显示在$_SESSION['icms']中的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;  

        } 
      } 
    }
  }   
}

最后,我的按钮功能在cart_functions.php中

header('Content-Type: application/json');
session_start();

if (isset($_GET['action'])) {
  $action = $_GET['action'];
  $prod   = $_GET['prod_id'];
  $prodname = 'product_'.$prod;
  $result;

  switch ($action) {
    case 'add':
        $result = add_prod($prod, $prodname);
    break;
    case 'plus':
        $result = plus_prod($prod, $prodname);
    break;
    case 'remove':
        $result = remove_prod($prod, $prodname);
    break;
    case 'delete':
        $result = delete_prod($prod, $prodname);
    break;
    default:
        $result = ['result'=>'error'];
    break;
  }
}

echo json_encode($result);

function add_prod($prod, $prodname){
  //sua função para add
 $_SESSION[$prodname] = 1;
 return ['result'=>'success'];
}

function plus_prod($prod, $prodname){
  $_SESSION[$prodname]++;
  return ['result'=>'success', 'val'=>$_SESSION[$prodname]];
}

function remove_prod($prod, $prodname){
  //sua função para remove
  $_SESSION[$prodname]--;
  return ['result'=>'success', 'val'=>$_SESSION[$prodname]];
}

function delete_prod($prod, $prodname){
  //sua função para delete
  $_SESSION[$prodname] = null;
  return ['result'=>'success'];
}

ajax调用中,需要这样发送fabricIDsizeID

{size_id: sizeID}{fabric_id: fabricID}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM