dsfdsfsdf45489 2016-10-10 14:12
浏览 13
已采纳

结合两个下拉选择来检索一个结果ajax / php

I got two dropdowns and i need to get both values to get data from my database.

$(document).ready(function(){

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


                }
            }); 
      //closing tags


    $('.size').on('change',function(){
        var sizeID = $(this).val();
        if(sizeID){
            $.ajax({
                type:'GET',
                url:'cart_functions.php',
                 dataType: 'json',
                data:{
                  size_id: sizeID
                },
                success:function(html){
                    $('.icms' + id).text(data.val);
                }
            }); 

//closing tags

i'm sending these both values to my calculate.php

<?php header('Content-Type: application/json');

 include_once '../incluedes/conn_cms.php'; 
 if(isset($_GET["size_id"],$_GET["fabric_id"])){
    $size_id=$_GET["size_id"] ;
    $fabric_id=$_GET["fabric_id"] ;

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

        $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 i select a fabric and then a size fabric value is my id and size is 50 or 45.

fabricid= 1 and size = 50 <-- i am sending this to my calculate.php So i want to get back the value into a session. and the result must be on a td..

<td class="icms'.$id.'">R$:'.$_SESSION['icms'] .'</td>

But its not working, i'm not good at ajax, can you tell me whats wrong and how can i fix these mess?

  • 写回答

2条回答 默认 最新

  • dongpo1846 2016-10-10 14:35
    关注

    Make sure both values are always sent with the request

    $(document).ready(function() {
        $('.fabric, .size').on('change', sendData);
    
        function sendData() {
            var fabricID = $('.fabric').val();
            var sizeID   = $('.size').val();
    
            if ( fabricID !== "" && sizeID !== "") {
                $.ajax({
                    type     : 'GET',
                    url      : 'cart_functions.php',
                    dataType : 'json',
                    data     : {
                        fabric_id: fabricID,
                        size_id: sizeID
                    }
                }).done(function(html) {
                    $('.icms' + this.id).text(data.val);
                });
            }
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题