title: CodeIgniter / Ajax . Posting parameters through ajax to CodeIgniter controller
body: <p>I am trying to post some variables to CodeIgniter controller function through ajax but ts resulting in an error message .</p>
  
  <p><strong>This is my javascript function</strong></p>
  
  <pre><code>$(document).ready(function()
   {
  
      $('.add_cart').click(function(){
        var quantity = $('#qty').val();
        var product_id = $(this).data("productid");
        var product_name = $(this).data("productname");
        var product_price = $(this).data("price");
  
        if(quantity != '' &amp;&amp; quantity &gt; 0)
            {
             $.ajax({
              url:"&lt;?php echo base_url(); ?&gt;products/add_product_to_cart",
              method:"POST",
              data:{product_id:product_id, product_name:product_name, product_price:product_price, quantity:quantity},
              success:function(data)
              {
                  alert('success');
              }
             });
            }
            else
            {
             alert("Please Enter quantity");
            }
  
          });
        });
  </code></pre>
  
  <p><strong>Here is my controller function.</strong> </p>
  
  <pre><code>    public function add_product_to_cart()
  {
  
      echo $_POST["product_id"];
  
  }
  </code></pre>
  
  <p>But when i track the whole process through firebug .Its giving the error that undefined variable product_id or does not print any thing when i try to echo it as given below.</p>
  
  <p>Here is the screen shot.<br>
  <a href="https://i.stack.imgur.com/6sRnt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6sRnt.jpg" alt="enter image description here"></a></p>
  
  <p>Can somebody help me about how to do this right or is there any special file that you need to include for ajax functions?</p>