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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分