dsh1102 2019-04-11 16:30
浏览 60
已采纳

Jquery - 带有ajax的产品比较复选框

I try to make a product comparison

  • 1 / the customer check one or several compare checkbox
  • 2 / when a checkbox is displayed, a message appears at the bottom
  • 3 / the value of checkbox is stored under a session

My problem is the ajax, it seems does not insert the checkbox value inside a session.

Thank you.

I started y that

<div><input type="checkbox" value="1" id="P1" /> P1</div>
<div><input type="checkbox" value="2" id="P2" /> P2</div>
<div><input type="checkbox" value="3" id="P3" /> P3</div>

<div id="container" style="display:none;"> 
    <div class="col-md-12 alert alert-info text-md-center">
        <a href="products_compare">Compare</div>
    </div>
</div>

the script

<script>
   $(function() {
$('input[type=checkbox]').change(function(){   
    var chkArray = [];   
    $('#container').html('');

    //put the selected checkboxes values in chkArray[]
    $('input[type=checkbox]:checked').each(function() {
        chkArray.push($(this).val());
    });


    //If chkArray is not empty show the <div> and create the list
    if(chkArray.length !== 0){
        $('#container').show();           
        $.each(chkArray, function(i, val) { 
            $('<p>').text(chkArray[i]).appendTo('#container');

             $.ajax({
                method: 'POST',
                url : "http://localhost/shop/ext/ajax/products_compare/compare.php",
                data : {product_id:chkArray[i]},
                success : function(resp){
                    alert("Product is added to be compared");
                }
            });               
        });
    }else{
        $('#container').hide();   
        $('#container').html('');
    }
});

})

the ajax file

$product_id = $_POST['product_id'];

if(!is_array($_SESSION['ids'])) {
  $_SESSION['ids'] = [];
} else {
  array_push($_SESSION['ids'], $product_id);
}
  • 写回答

1条回答 默认 最新

  • dtdh11647 2019-04-12 07:49
    关注

    You need to add session_start(); before you use the $_SESSION variable. You should check if $_SESSION['ids'] is set by using isset().

    session_start();
    $product_id = $_POST['product_id'];
    $_SESSION['ids'] = $product_id;
    

    This should work now, please check it.

    Nevertheless, I would restructure your JavaScript code in order to reduce the number of requests send. At the moment, you are sending a separate request for every checkbox checked instead of just sending one array (just remove the $.each loop).

    if(chkArray.length !== 0){      
        $.ajax({
                method: 'POST',
                url : "http://localhost/test/test2.php",
                data : {product_id: chkArray},
                success : function(resp){
                    alert("Product is added to be compared");
                }
            });  
    }
    

    Please keep in mind that you are now adding an array to the array every time you do a POST.

    Array
    (
        [0] => Array
            (
                [0] => 1
            )
    
        [1] => Array
            (
                [0] => 1
                [1] => 3
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。