Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this javascript that stores able values in an array:

<script>
$(function(){
    $('#preview').click(function(){
    var thesum=0;
    var rowid=[];
    var rowfields=[];
    var supplier = document.getElementById("sid").value;
    var terms=document.getElementById("terms").value;
    var podate=document.getElementById("date1").value;
    var reqdate=document.getElementById("date2").value;
    var contp=document.getElementById("contactp").value;
    var count = ($('#listOfProducts tr').length);
    //loop start
    var i=0;
    grid=$('#listOfProducts input[type="checkbox"]:checked').each(function(){
    var $row = $(this).parents('tr'); 
    var $trid =$(this).closest('tr').attr('id');
    rowid[i]=$trid; 
    rowfields.push({itemname: $row.find('td:eq(0)').text(),       productname:$row.find('td:eq(1)').text(), productdesc: $row.find('td:eq(2)').text(), unitprice:$row.find('td:eq(3)').text(), quantity:$row.find('td:eq(5) input').val(), amount:$row.find('td:eq(6) input').val()});
    i++;
    });//each close 

var dataString = JSON.stringify(rowfields); 
   });//preview click close
 });//function close
 </script>

i need to pass the rowfields array to my controller so i can start manipulating the data on other functions. Can you please help me with this?

share|improve this question
USE AJAX POST TO SEND THE DATA TO CONTROLLOR – Sundar Aug 5 at 4:11
i do not actually know how :( – Umpong_1947676 Aug 5 at 4:17
add comment (requires an account with 50 reputation)

1 Answer

Jquery ajax

$(document).ready(function(){

    $('#preview').click(function(){
    var thesum=0;
    var rowid=[];
    var rowfields=[];
    var supplier = document.getElementById("sid").value;
    var terms=document.getElementById("terms").value;
    var podate=document.getElementById("date1").value;
    var reqdate=document.getElementById("date2").value;
    var contp=document.getElementById("contactp").value;
    var count = ($('#listOfProducts tr').length);
    //loop start
    var i=0;
    grid=$('#listOfProducts input[type="checkbox"]:checked').each(function(){
    var $row = $(this).parents('tr'); 
    var $trid =$(this).closest('tr').attr('id');
    rowid[i]=$trid; 
    rowfields.push({itemname: $row.find('td:eq(0)').text(),       productname:$row.find('td:eq(1)').text(), productdesc: $row.find('td:eq(2)').text(), unitprice:$row.find('td:eq(3)').text(), quantity:$row.find('td:eq(5) input').val(), amount:$row.find('td:eq(6) input').val()});
    i++;
    });//each close 

    var dataString = JSON.stringify(rowfields);

    //ajax function 
    //http://api.jquery.com/jQuery.ajax/
      $.ajax({  
          //controller path
          url: "/path/to/url",  
          type: "POST",  
          dataType: "json",  
          contentType: "json",  
          data: dataString
          success: function(response){              
           console.log(response);
          },  
          error: function(response){  

              console.log(response);
          }  
        }); //ajax close



   });//preview click close
 });//function close
share|improve this answer
in case you can't get the input values into $this->input->post() then try like this file_get_contents('php://input') – Sundar Aug 5 at 4:26
Here's my controller function... how can i check if the data has been passed? public function retrievepoform(){ $myPostData =$this->input->post(); echo json_encode($myPostData); $this->load->view('PurchaseOrderPreview'); } – Umpong_1947676 Aug 5 at 4:40
In server side you need to convert the string back to JS using json_decode() function, if you want to use it as array. If you only need to store in database better store the data as text in one column. – Nish Aug 5 at 4:49
get the values like this $data = json_decode(file_get_contents('php://input')); and echo like below print_r($data); – Sundar Aug 5 at 4:58
I still cant do it :( I am think of other possible approches aside from ajax... any suggestion? – Umpong_1947676 Aug 5 at 13:46
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.