2
votes

I am trying to get a value from an AJAX post that always seems to be empty. I already tried printing the value before doing the post and it works fine but I cannot get the value via $this->input->post()

HTML

 <?php if ($product_info->stock > '1'){?>
 <button class="btn btn-lg btn-primary" product_id = "<?php echo $product_info->id;?>" id = "addtocart"><i class="fa fa-shopping-cart"></i>Add to Cart</button><?php}?>

Javascript

$(document).ready(function(){
    $("#addtocart").click(function(){
        var productid = $(this).attr('product_id');

        $.ajax({
            type: "POST",
            url: "<?php echo base_url();?>shoppingcart",
            data: productid,
            cache: true,
            success: function(productid){
                $("#cartstatus").html('<div class="alert alert-success"><strong>Success! </strong>Item has been added to the cart.</div>');
            }, error: function(productid){
                $("#cartstatus").html('<div class="alert alert-danger"><strong>Opps! </strong>Something went wrong.</div>')
            }
        });
    });
});

Controller

$productcart = array();

        if($this->session->userdata('cartsession')){
            $cartsession = $this->session->userdata('cartsession');

            foreach($cartsession as $id=>$val){
                $productcart[$id] = $val;
            }
        }

        $productcart[$this->input->post('productid')] = 50; -->sample value
        $this->session->set_userdata('cartsession', $productcart);
        $cartsession = $this->session->userdata('cartsession');
2
Do you know if CSRF is turned on? That will stop the post from working. I had a similar problem in the past and found I had to move $this->input->post('productid') to populate a variable before I could use it as in $myVar = $this->input->post('productid') (that is separate from CSRF) - hayres

2 Answers

1
votes

data params have certian format for sending values to the server using ajax.

Try this ...

$(document).ready(function(){
    $("#addtocart").click(function(){
        var productid = $(this).attr('product_id');

    $.ajax({
        type: "POST",
        url: "<?php echo base_url();?>shoppingcart",
        data: {productid : productid},
        cache: true,
        success: function(productid){
            $("#cartstatus").html('<div class="alert alert-success"><strong>Success! </strong>Item has been added to the cart.</div>');
        }, error: function(productid){
            $("#cartstatus").html('<div class="alert alert-danger"><strong>Opps! </strong>Something went wrong.</div>')
        }
    });


   });
});
0
votes

Instead having your own attribute 'product-id', use HTML5's data attribute like 'data-product_id', so the HTML code looks like:

<button class="btn btn-lg btn-primary" data-product_id="<?php echo $product_info->id;?>" id="addtocart"><i class="fa fa-shopping-cart"></i>Add to Cart</button><?php}?>

Also, assign class name to the elements if you have more than one 'Add to Cart' buttons to group them, so it looks like:

<button class="btn btn-lg btn-primary" data-product_id="<?php echo $product_info->id;?>" class="addtocart"><i class="fa fa-shopping-cart"></i>Add to Cart</button><?php}?>

It's time to make use of the data attribute using jQuery:

var product_id = $(this).data('product_id');

Now, the whole code looks like

$(document).ready(function(){
    $(".addtocart").click(function(){
    var product_id = $(this).data('product_id');
        $.ajax({
            type: "POST",
            url: "<?php echo base_url();?>shoppingcart",
            data: product_id,
            cache: true,
            success: function(response){
                $("#cartstatus").html('<div class="alert alert-success"><strong>Success! </strong>Item has been added to the cart.</div>');
            }, error: function(response){
                $("#cartstatus").html('<div class="alert alert-danger"><strong>Oops! </strong>Something went wrong.</div>')
            }
        });
    });
});

Hope it works.