2013-08-05 78 views
1

我有這個JavaScript數組中的存儲能力值:需要從javascript傳遞數組值到PHP控制器代碼點火器

<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> 

我需要的rowfields數組傳遞給我的控制器,這樣我就可以開始操縱關於其他功能的數據。你能幫我解決這個問題嗎?

+0

使用AJAX POST發送的數據CONTROLLOR – Sundar

+0

我真的不知道該怎麼:( –

回答

1

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 
+0

如果你不能得到的輸入值到$這個 - >輸入 - >後()然後嘗試像這樣file_get_contents('php:// input') – Sundar

+0

這裏是我的控制器函數...我如何檢查數據是否已通過公共職能retrievepoform(){ \t $ myPostData = $ this-> input-> post(); \t echo json_encode($ myPostData); \t $ this-> load-> view('PurchaseOrderPreview'); \t} –

+0

在服務器端,如果要將其用作數組,則需要使用json_decode()函數將字符串轉換回JS。如果您只需要將數據存儲在數據庫中,則可以將數據更好地存儲爲一列中的文本。 – Nishanthan

相關問題