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

Hi everyone I have this javascript that has to pass some variables including an array. My problem is that I cannot pass these values using the uRL because i might deal with many values. I am trying to use ajax JSON but i just cannot retrieve the values: Here is my javascript:

$(function(){
      $('#preview').click(function(){
         var thesum=0;
         var rowid=[];
         var rowfields=[];
             var supplier = document.getElementById("sid").value; //need to pass
             var terms=document.getElementById("terms").value; //need to pass

             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 $tbldata=JSON.stringify(rowfields);//need to pass
                window.location = '<?php echo base_url();?>index.php/main/retrievepo';// this is where i should get the passeddata
    });//preview click close
 });//function close

Here is my function, located in a PHP Controller (i am using codeigniter)

public function retrievepo()
{
// should recieve data here
$this->load->view('PurchaseOrderPreview');
}

Any help please? I've been stacked from here for so long...

share|improve this question
you need to use ajax if you want to pass the data from javascript to PHP – Drixson Oseña Aug 6 at 6:53
with in controller function, use post library to get the perameters – Suleman Aug 6 at 6:53
can you give me a fiddle of this guys... please – Umpong_1947676 Aug 6 at 7:01

1 Answer

up vote 1 down vote accepted

Why dont you try like this:

$(function(){
      $('#preview').click(function(){
         var thesum=0;
         var rowid=[];
         var rowfields=[];
         var supplier = document.getElementById("sid").value; //need to pass
         var terms=document.getElementById("terms").value; //need to pass

         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 tbldata=JSON.stringify(rowfields);//need to pass
     $.post('/index.php/main/retrievepo',{"tbldata" : tbldata},function(response) 
     {
           //Load the response here to any div after ajax call     
           //Eg: $('#div_id').html(response);
     });//preview click close
 });
});

PHP Controller:

<?
public function retrievepo()
{
// should recieve data here
$data= $this->input->post('tbldata');
//pass the received post variables to view and access them inside your view.php
  $this->load->view('PurchaseOrderPreview',$data);
 }
    ?>
share|improve this answer
i got this response in the div id i have specified "[{\"itemname\":\"TSauce 250 mL\",\"productname\":\"Tomato Sauce\",\"productdesc\":\"Filipino Style\",\"unitprice\":\"24\",\"quantity\":\"1\",\"amount\":\"24\"},{\"itemname\"‌​:\"Tsauce 100 mL\",\"productname\":\"Tomato Sauce\",\"productdesc\":\"Filipino Style\",\"unitprice\":\"50\",\"quantity\":\"1\",\"amount\":\"50\"}]" but how can i get this on my controller? Can i just call the controller function for the response? – Umpong_1947676 Aug 6 at 7:16
I tried this in the controller, and it simply returns "false" public function retrievepo() { $data= $this->input->post('tbldata'); $this->load->view('PurchaseOrderPreview'); echo json_encode($data); //echo $jqueryVariable; } – Umpong_1947676 Aug 6 at 7:19
I want to assign the data to an array at the controller, can I do this? – Umpong_1947676 Aug 6 at 7:21
Try this: public function retrievepo() { $data= $this->input->post('tbldata'); echo "<pre>"; print_r($data); echo "</pre>"; } – Aditya Aug 6 at 7:21
If this worked, please up vote the answer , thanks :) – Aditya Aug 6 at 7:27
show 5 more comments

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.