如何在js中传递值

时间:2019-04-11 20:48:38

标签: javascript php jquery ajax

在我的compare.php中,我在NULL VALUE上有此$_SESSION 你知道如何解决这个问题吗?

谢谢

js函数:

$(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');
            });


            $('#rectButton').on('click', function (e) {     
             $.ajax({
                method: 'POST',
                url : "http://localhost/shop/ext/ajax/products_compare/compare.php",
                data : {product_id:chkArray},
/*
                success : function(resp){
                    alert("Product is added to be compared" );
                }
*/                
             });
           });

        }else{
            $('#container').hide();   
            $('#container').html('');
        }
    });    
})             
</script>

<button id="rectButton" class="btn"><a href="compare.php">Compare</a></button> 

我的Ajax文件

 $product_id = $_POST['product_id'];

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

ajax文件的结果是:

product_id[]: 12
product_id[]: 10
product_id[]: 9

我的文件ajax结果上的compare.php。

var_dump($_SESSION] ===> NULL
var_dump($_SESSION]N['ids']; ===> NULL


result I see:
array(1) { ["ids"]=> array(5) { [0]=> array(1) { [0]=> string(2) "10" } [1]=> array(2) { [0]=> string(2) "10" [1]=> string(1) "9" } [2]=> array(1) { [0]=> string(1) "9" } [3]=> array(2) { [0]=> string(2) "12" [1]=> string(1) "9" } [4]=> array(3) { [0]=> string(2) "12" [1]=> string(2) "11" [2]=> string(1) "9" } } }

获得好的结果:

[4]=> array(3) { [0]=> string(2) "12" [1]=> string(2) "11" [2]=> string(1) "9" } 

1 个答案:

答案 0 :(得分:0)

由于$_POST['product_id']是一个数组,因此您不应将整个数组作为单个项推入会话变量,这就是为什么要获得二维数组的原因。您应该简单地将新数组分配给会话变量,因为它包含了用户检查过的所有项目。

$_SESSION['ids'] = $_POST['product_id'];

您不应将点击处理程序绑定在更改处理程序内部。您应该独立绑定两个处理程序,并且它们都可以使用全局chkArray变量。

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

    //put the selected checkboxes values in chkArray[]
    chkArray = $('input[type=checkbox]:checked').map(function() {
      return this.value;
    }).get();

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

  $('#rectButton').on('click', function(e) {
    $.ajax({
      method: 'POST',
      url: "//localhost/shop/ext/ajax/products_compare/compare.php",
      data: {
        product_id: chkArray
      },
      /*
      success: function(resp) {
        alert("Product is added to be compared");
      }
      */
    });
  });

})