title: shopping cart in codeigniter using ajax
body: <p>Hi I am new with ajax and having problem. I am trying to make a shopping cart using ajax.I don't know what is wrong with my code please help me out.
  When I click the add button an alert comes 'No success' and nothing happen , I am not able add items to cart.
  Thanks for helping me.</p>
  
  <p>This is my view</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Codeigniter cart class&lt;/title&gt;
      &lt;link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"&gt;&lt;/script&gt;
      &lt;link href='http://fonts.googleapis.com/css?family=Raleway:500,600,700' rel='stylesheet' type='text/css'&gt;
        &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;css/style.css"&gt;
  
      &lt;script type="text/javascript"&gt;
          $(document).ready(function() {
          $("#myform").submit(function(event) {
          event.preventDefault();
  
          var insert_data= $(this).serializeArray();
          $.ajax({
          url:  "&lt;?php echo base_url(); ?&gt;" + "index.php/shopping2/add",  
          dataType: 'json',
          //type: "POST",
          data: insert_data,
  
              success:function(response) {
  
  //if (response){
     //var res = JSON.parse(response);
      var res = response;
      if(res.status == 200){
  
          window.location.href="http://127.0.0.1/codeigniter_cart2/index.php/shopping2";
      }else{
  
          alert(res.msg);
      }
  //}
  //else{ 
     // alert('sorry');
  //}
                  }
  
  });
          });
          });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  
    &lt;div id='content'&gt;
      &lt;div class="row"&gt; 
  
      &lt;div class="col-sm-5"&gt;
          &lt;h2 align="center"&gt;Items&lt;/h2&gt;
          &lt;?php
  
  
          ?&gt;
  
                   &lt;table id="table" border="0" cellpadding="5px" cellspacing="1px"&gt;
  
          &lt;?php
          foreach ($products as $product) {
              $id = $product['serial'];
              $name = $product['name'];
  
              $price = $product['price'];
              ?&gt;
  
  
                    &lt;tr class="well"&gt;
                   &lt;td style="padding-left:15px;"&gt;&lt;?php echo $name; ?&gt;&lt;/td&gt;
  
                      &lt;td&gt;
                          Rs. &lt;?php echo $price; ?&gt;&lt;/td&gt;
                      &lt;?php
                      ?&gt;
  
                      &lt;?php
                      echo form_open('',array('id' =&gt; 'myform'));
                      echo form_hidden('id', $id);
                      echo form_hidden('name', $name);
                      echo form_hidden('price', $price);
                      ?&gt; &lt;!--&lt;/div&gt;--&gt; 
  
                      &lt;?php
                      $btn = array(
                          'class' =&gt; 'fg-button teal',
                          'value' =&gt; 'Add',
                          'name' =&gt; 'action',
                          'id' =&gt; 'add_button'
                      );
                      ?&gt;
                     &lt;td&gt;
                      &lt;?php
                      // Submit Button.
                      echo form_submit($btn);
                      echo form_close();
                      ?&gt;
                  &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;?php } ?&gt;
                  &lt;/table&gt;
  
                  &lt;/div&gt;
  
      &lt;div class="col-sm-7"&gt;
     &lt;!-- &lt;div id="cart" &gt;--&gt;
  
              &lt;h2 align="center"&gt;Items on  Cart&lt;/h2&gt;
  
  
              &lt;div&gt; 
          &lt;?php  $cart_check = $this-&gt;cart-&gt;contents();
  
  
           if(empty($cart_check)) {
           echo 'To add products to your shopping cart click on "Add" Button'; 
           }  ?&gt; &lt;/div&gt;
  
              &lt;table id="table" border="0" cellpadding="5px" cellspacing="1px"&gt;
                &lt;?php
                // All values of cart store in "$cart". 
                if ($cart = $this-&gt;cart-&gt;contents()): ?&gt;
                  &lt;div id="addcart"&gt;
                  &lt;tr id= "main_heading" class="well"&gt;
  
                      &lt;td style="padding-left:15px;"&gt;&lt;?&gt;Name&lt;/td&gt;
                      &lt;td&gt;Price(Rs)&lt;/td&gt;
                      &lt;td&gt;Qty&lt;/td&gt;
                      &lt;td&gt;Amount&lt;/td&gt;
                      &lt;td&gt;Remove&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;?php
                   // Create form and send all values in "shopping/update_cart" function.
                  echo form_open('shopping2/update_cart');
                  $grand_total = 0;
                  $i = 1;
  
                  foreach ($cart as $item):
  
  
                      echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
                      echo form_hidden('cart[' . $item['id'] . '][rowid]', $item['rowid']);
                      echo form_hidden('cart[' . $item['id'] . '][name]', $item['name']);
                      echo form_hidden('cart[' . $item['id'] . '][price]', $item['price']);
                      echo form_hidden('cart[' . $item['id'] . '][qty]', $item['qty']);
                      ?&gt;
                      &lt;tr class="well" id="addcart"&gt;
  
                          &lt;td style="padding-left:15px;"&gt;
                    &lt;?php echo $item['name']; ?&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;?php echo number_format($item['price'], 2); ?&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                          &lt;?php echo form_input('cart[' . $item['id'] . '][qty]', $item['qty'], ' type="number" max="99" min="1" value="1" style="width:50px;"'); ?&gt;
                          &lt;/td&gt;
                      &lt;?php $grand_total = $grand_total + $item['subtotal']; ?&gt;
                          &lt;td&gt;
                              Rs &lt;?php echo number_format($item['subtotal'], 2) ?&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
  
                          &lt;?php 
                          // cancle image.
                          $path = "&lt;img src='http://127.0.0.1/codeigniter_cart2/images/cart_cross.jpg' width='25px' height='20px'&gt;";
                          echo anchor('shopping/remove/' . $item['rowid'], $path); ?&gt;
                          &lt;/td&gt;
                   &lt;?php endforeach; ?&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td style="padding-left:30px;"&gt;&lt;b&gt;Order Total: Rs &lt;?php 
  
                      //Grand Total.
                      echo number_format($grand_total, 2); ?&gt;&lt;/b&gt;&lt;/td&gt;
  
  
                      &lt;td colspan="5" align="right"&gt;&lt;input type="button" class ='fg-button teal' value="Clear cart" onclick="window.location = 'shopping2/remove'"&gt;
  
                          &lt;?php //submit button. ?&gt;
                          &lt;input type="submit" class ='fg-button teal' value="Update Cart"&gt;
                          &lt;?php echo form_close(); ?&gt;
  
  
                          &lt;/td&gt;
                  &lt;/tr&gt;&lt;/div&gt;
  &lt;?php endif; ?&gt;
          &lt;/table&gt;
  
  
      &lt;/div&gt;
  
  
     &lt;!-- &lt;div id="products_e" align="center"&gt;--&gt;
  
              &lt;!--&lt;/div&gt;--&gt;
  
  
  
     &lt;!-- &lt;/div&gt;--&gt;
    &lt;/div&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my controller</p>
  
  <pre><code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  </code></pre>
  
  <p>class Shopping2 extends CI_Controller {</p>
  
  <pre><code>public function __construct()
  {
      parent::__construct();
      //load model
      $this-&gt;load-&gt;model('billing_model');
              $this-&gt;load-&gt;library('cart');
  }
  
  public function index()
  {   
  
      $data['products'] = $this-&gt;billing_model-&gt;get_all();
  
      $this-&gt;load-&gt;view('shopping_view2', $data);
  }
  function add()
  {
          $insert_data = array(
          'id' =&gt; $this-&gt;input-&gt;post('id'),
          'name' =&gt; $this-&gt;input-&gt;post('name'),
          'price' =&gt; $this-&gt;input-&gt;post('price'),
          'qty' =&gt; 1
      );  
  
      $this-&gt;cart-&gt;insert($insert_data);
     //$success = $this-&gt;cart-&gt;insert($insert_data);
       $cart_check = $this-&gt;cart-&gt;contents();
  
   if(!empty($cart_check)){ 
      //$this-&gt;cart-&gt;contents(insert_data);
      $res = array('status' =&gt; 200, 'msg' =&gt; 'success');
  
  }else{
      $res = array('status' =&gt; 500, 'msg' =&gt; 'No success');
  }
  
  echo json_encode($res);
  //echo $data[0]['value'];
  //redirect('shopping2');
  }
  
  
      function remove($rowid) {
                  // Check rowid value.
      if ($rowid==="all"){
  
          $this-&gt;cart-&gt;destroy();
      }else{
  
          $data = array(
              'rowid'   =&gt; $rowid,
              'qty'     =&gt; 0
          );
  
          $this-&gt;cart-&gt;update($data);
      }
  
  
      redirect('shopping2');
  }
  
      function update_cart(){
  
              // Recieve post values,calcute them and update
              $cart_info =  $_POST['cart'] ;
      foreach( $cart_info as $id =&gt; $cart)
      {   
                  $rowid = $cart['rowid'];
                  $price = $cart['price'];
                  $amount = $price * $cart['qty'];
                  $qty = $cart['qty'];
  
                      $data = array(
              'rowid'   =&gt; $rowid,
                              'price'   =&gt; $price,
                              'amount' =&gt;  $amount,
              'qty'     =&gt; $qty
          );
  
          $this-&gt;cart-&gt;update($data);
      }
      redirect('shopping2');        
  }   
  
  }
  </code></pre>
  
  <p>Please help me how to add items in the cart using ajax.</p>