【问题标题】:Pass Javascript Variables tp PHP Controller in Code Igniter在 Codeigniter 中将 Javascript 变量传递给 PHP 控制器
【发布时间】:2013-08-06 06:47:24
【问题描述】:

大家好,我有这个 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');
}

有什么帮助吗?我从这里堆了这么久……

【问题讨论】:

  • 如果要将数据从javascript传递到PHP,则需要使用ajax
  • 带有in控制器功能,使用post库获取参数
  • 你能给我这些家伙的小提琴...拜托

标签: php javascript jquery ajax codeigniter


【解决方案1】:

你为什么不试试这样:

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

【讨论】:

  • 我在我指定"[{\"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\"}]" 的 div id 中得到了这个响应,但是我怎样才能在我的控制器上得到这个?我可以只调用控制器函数来响应吗?
  • 我在控制器中试过这个,它只是返回“false”public function retrievepo() { $data= $this-&gt;input-&gt;post('tbldata'); $this-&gt;load-&gt;view('PurchaseOrderPreview'); echo json_encode($data); //echo $jqueryVariable; }
  • 我想将数据分配给控制器的数组,我可以这样做吗?
  • 试试这个: public function retrievepo() { $data= $this->input->post('tbldata');回声“
    ”;打印_r($数据);回声“
    ”; }
  • 非常感谢...现在我的问题是将其存储在数组中
猜你喜欢
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多