2

I am developing a shopping cart using codeigniter. When i am sending data via ajax to controller it doesn't works. here is my ajax code i am using You can check my demo ebsite in this following link

http://cableandmedia.com/ayurstore/products

$(".add_to_cart").click(function(event) {

  var id=$(this).data('id'); 
 
  var qty=$("#item_"+id).val();
 
  
  $.ajax({
            type: 'POST',
            url: '<?php echo base_url("ajax_controller/add_to_cart/'+id+'/'+qty+'")?>',
            data: { id:id }, 
            success:function(response){
            $("#total_items").html(response);
            $(".view_cart").click();
     }
  });
<input type="number"  value="1" id="item_<?php echo $row->product_id; ?>"  />
<a  id="edit_product" data-id="<?php echo $row->product_id; ?>" class="add_to_cart">Add to Cart</a>

When I am alert(qty) and alert (id) i am getting the result. But i am not getting values in my ajax_controller.

This is my controller

public function add_to_cart($pid,$qty)
	{
		$this->load->model('product_model');
		$query=$this->product_model->get_product($pid);
		
		foreach ($query->result() as $row)
		{
    		$name=$row->product_name;
    		$price=$row->price;
    		$img=$row->img_name;
		}
		
		
			$data = array(
               'id'      => $pid,
               'qty'     => $qty,
               'price'   => $price,
               'name'    => $name,
               'options' => array('Status' => 'New')
            );

			$this->cart->insert($data);
			
			echo count($this->cart->contents());
	}

4
  • please share your controller code ajax_controller/add_to_cart Commented May 22, 2017 at 6:14
  • Please share your ajax_controller controller code @Yadhu Babu Commented May 22, 2017 at 6:21
  • My issue is that i need to grt that product id and $qty in my ajax controller Commented May 22, 2017 at 6:29
  • You can see my answer @YadhuBabu Commented May 22, 2017 at 6:35

4 Answers 4

3

Missing argument 1 for Ajax_controller::add_to_cart() is the php error that was encounterd.So the problem is with url. product id and quantity are not passed with url.So change the url as below.

           <?php echo base_url("ajax_controller/add_to_cart/")?>"+id+"/"+qty
Sign up to request clarification or add additional context in comments.

Comments

3

$(".add_to_cart").click(function(event) {

  var id=$(this).data('id'); 
 
  var qty=$("#item_"+id).val();
 
  
  $.ajax({
            type: 'POST',
            url: '<?php echo base_url("ajax_controller/add_to_cart/")?>',
            data: { pid:id,qty:qty }, 
            success:function(response){
            $("#total_items").html(response);
            $(".view_cart").click();
     }
  });
<input type="number"  value="1" id="item_<?php echo $row->product_id; ?>"  />
<a  id="edit_product" data-id="<?php echo $row->product_id; ?>" class="add_to_cart">Add to Cart</a>

Change Controller code like

public function add_to_cart()
{
    $pid = $this->input->post('pid');
    $qty = $this->input->post('qty');
    $this->load->model('product_model');
    $query=$this->product_model->get_product($pid);

    foreach ($query->result() as $row)
    {
        $name=$row->product_name;
        $price=$row->price;
        $img=$row->img_name;
    }


        $data = array(
           'id'      => $pid,
           'qty'     => $qty,
           'price'   => $price,
           'name'    => $name,
           'options' => array('Status' => 'New')
        );

        $this->cart->insert($data);

        echo count($this->cart->contents());
}

Comments

3

Change ajax url You are using java script variables inside the php tag .

$(".add_to_cart").click(function(event) {

  var id=$(this).data('id'); 
 
  var qty=$("#item_"+id).val();
 
  
  $.ajax({
            type: 'POST',
            url: "<?php echo base_url('ajax_controller/add_to_cart/')?>"+id+"/"+qty,
            data: { id:id }, 
            success:function(response){
            $("#total_items").html(response);
            $(".view_cart").click();
     }
  });
<input type="number"  value="1" id="item_<?php echo $row->product_id; ?>"  />
<a  id="edit_product" data-id="<?php echo $row->product_id; ?>" class="add_to_cart">Add to Cart</a>

Comments

3

You are passing the value from as ajax as post so try to get it using

In you ajax

    jQuery.ajax({
    type: "POST",
    url: '<?php echo base_url("ajax_controller/add_to_cart")'?>,
    dataType: 'json',
    data: {id: id, qty: qty},
    success: function(res)

Write below code in you controller

$this->input->post('id');
$this->input->post('qty');

2 Comments

typo missing to close double quotes close in url: and you need to escape the double quotes if you use double quotes inside the double quotes
still one typo in this line find it yourself url: '<?php echo base_url("ajax_controller/add_to_cart")'?>,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.