将Javascript变量传递给Codeigniter中的PHP控制器

时间:2013-08-06 06:47:24

标签: php javascript jquery ajax codeigniter

大家好,我有这个javascript,必须通过一些变量,包括一个数组。我的问题是我无法使用uRL传递这些值,因为我可能会处理许多值。我试图使用ajax JSON,但我无法检索值:这是我的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

这是我的函数,位于PHP控制器(我正在使用codeigniter)

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

请帮忙吗?我已经从这里堆积了这么久......

1 个答案:

答案 0 :(得分:2)

为什么不尝试这样:

$(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控制器:

<?
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);
 }
    ?>