0

This is my imple code on submit of form. Where I want to insert table data values in database through ajax. But it's not going to controller.

$('#submit').click(function(){

 var TableData = new Array();
 $('#cart_details tr').each(function(row, tr){

 TableData[row]={
    "productname" : $(tr).find('td:eq(0)').text()
    , "quantity" :$(tr).find('td:eq(1)').text()
    , "unit" : $(tr).find('td:eq(2)').text()
    , "unit_rate" : $(tr).find('td:eq(3)').text()
  }

   });


TableData.shift();

//TableData = $.toJSON(TableData);
var TableData = JSON.stringify(TableData);

alert(TableData);
var followurl='<?php echo base_url()."index.php/purchase/save_product";?>';

$.ajax({
   type: "POST",                     
   url:followurl,                                                 
   data: TableData,
   datatype : "json",

   cache: false,
   success: function (data) {
     alert("dsad"+data);
   }
   });

  });

When I stringify tabledata array output is like this..

  [{"productname":"Copper Sulphate","quantity":"1","unit":"1","unit_rate":"100"},
  {"productname":"Hypta Hydrate","quantity":"1","unit":"1","unit_rate":"100"}]

My question is why it's not going to controller? it's because of array object or something else??

Tabledata is javascript object array . Am I right??

Kedar B
  • 774
  • 5
  • 18
  • 47

3 Answers3

0

Use

$.ajax({

instead of

$.post({

use this code

$.ajax({
   type: "POST",                     
   url:followurl,                                                 
   data: {TableData : TableData},
   cache: false,
   success: function (data) {
     alert("dsad"+data);
   }
   });

check the Documentation jquery.post

The syntax for $.post is

$(selector).post(URL,data,function(data,status,xhr),dataType)

You don't have to define the type ,

but here you are using the $.ajax mixing with $.post

this is the $.ajax function syntax

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

SO change the $.post to $.ajax and try

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
0

As you can read in the documentation, you can pass an object to data. I think you'd make things easier and simpler for you if you followed that approach.

...
//TableData = $.toJSON(TableData); NO!!!
//var TableData = JSON.stringify(TableData); NO!!!

//alert(TableData);
var followurl='<?php echo base_url()."index.php/purchase/save_product";?>';

$.ajax({
   type: "POST",                     
   url:followurl,                                                 
   data: { 
    dataTable: TableData
   },
   datatype : "json",
   cache: false,
   success: function (data) {
     alert(data);
   }
   });

});

Very simple example (without validation or anything of the kind) of index.php/purchase/save_product

$data = $_POST["dataTable"];
echo $data[0]["productname"];// Sending back the productName of the first element received.
die();

As you can see, you could access data in your index.php/purchase/save_product file very easily if you followed this approach.

Hope it helps.

acontell
  • 6,792
  • 1
  • 19
  • 32
0

Hi It look like you are using some CMS or Framework. Can you please let us know which framework or CMS you are using. I would then be able to sort out this issue. It looks like you are using Code Ignitor. If its so then i hope this would help you

$.post( "<?php echo base_url();?>index.php/purchase/save_product", function(data) {
  alert( "success" );
}, 'html') // here specify the datatype
.fail(function() {
  alert( "error" );
})

in Your case your ajax call must look like

var followurl="<?php echo base_url();?>index.php/purchase/save_product";

$.ajax({
   type: "POST",                     
   url:followurl,                                                 
   data: TableData,
   datatype : "json",

   cache: false,
   success: function (data) {
     alert("dsad"+data);
   }
   });

  });

Error Seems to be in your followUrl please try using as its in mine code