2013-03-01 32 views
0

我有一個視圖,它計算要編輯的條目,使用javascript刪除。該id是根據打勾的複選框計算出來的,並存儲在一個數組中,需要發送到控制器方法來編輯或刪除...我在某處理解變量不應該從視圖發送到Codeigniter中的控制器。我怎樣才能做到這一點?如何在本例中避免將變量從視圖發送到控制器?

查看

function checkedAll() { 
    var rowlength=document.getElementById("check").rows.length; 
    z=document.getElementById("check").getElementsByTagName("input")[0].checked; 

    for(var i=1;i<rowlength-1;i++) 
    { 
     document.getElementById("check").getElementsByTagName("input")[i].checked = z; 
    } 
} 

function del(){ 
    var rowlength=document.getElementById("check").rows.length; 
    var id = new Array(); 

    for(var i=1;i<rowlength-1;i++) 
    { 
     var t = document.getElementById("check").getElementsByTagName("input")[i].checked; 
    var y = document.getElementById("check").rows[i].cells; 
     id[i]=y[0].innerHTML; 
    } 
} 
</script> 
</head> 

<body> 
    <table id="check" > 
     <tr> 
      <th>S.No</th> 
      <th>Name</th> 
      <th>Age</th> 
      <th>Qualification</th> 
      <th> <input type="checkbox" onclick='checkedAll()'/> </th> 
     </tr> 

     <?php 
     $check=0; 
     $flag=0;  
     $my_checkbox=array(); 

     foreach($forms as $ft): ?> 
      <tr id="<?php echo $check;?>" class="<?php echo $d ?>"> 
       <td > <?php echo $ft['serial']; ?> </td> 
       <td> <?php echo $ft['name'] ;?></td> 
       <td> <?php echo $ft['age'] ;?></td> 
       <td> <?php echo $ft['qualification'] ;?></td> 
       <td> <input type="checkbox" /></td> 
      </tr> 
      <?php $check++ ?>    
     <?php endforeach ?> 

     <tr> 
      <td colspan="5" align="center"> 
       <button type="button" name="create" id='but' value="Create" 
         onclick = "Redirect();" >Create </button> 
       <button type="button" name="edit" onclick="edit();" id='but' >Edit </button> 
       <button type="button" name="delete" id='but' onclick="del(); " >Delete </button> 
      </td> 
     </tr> 

    </table> 

    <form action="<?php $this->load->helper('url');echo site_url('form/edit');?>" method="POST" id="myForm" > 
     <input type="hidden" name="snap" id="snap"> 
    </form> 
</body> 

+0

當然,你可以發送變量來控制你要麼可以在URL中使用與PARAMS正常的GET請求或你要麼可以使用POST請求,並張榜公佈變量到控制器 – 2013-03-01 11:01:02

+0

另一件事,你應該加載你的控制器上的助手而不是你的視圖,所以你應該從視圖中刪除這個「$ this-> load-> helper('url')」並將其粘貼到方法上調用該視圖,或者如果你真的使用它很多隻是自動加載它 – 2013-03-01 11:03:29

+0

是的,將該幫助器移動到控制器..如何將數據從視圖發佈到控制器方法? – Skeptic 2013-03-01 12:09:11

回答

0

在接受這個數組和處理它,使用jQuery AJAX將此數據發送到控制器,控制器創建方法......無需重新加載,可直接調用沒有問題。

在另一方面,可以創建其直接稱爲控制器的方法,以及處理陣列重定向回視圖後效仿呆在那裏。我不建議這樣做,因爲它是5-6歲的方法。

如果您需要更詳細的說明,請給我留言。

下面是示例代碼:

var data = //your variable to be sent 
$.ajax({ 
    type: "POST", 
    url: "/controller/method", 
    data: data, 
    success: function(result) { 
     // data is returned by controller 

    }, 
    error: function(){ 
     console.log('ERROR'); 
     //for errors thrown by PHP exceptions and other backend features 
     //or you can return result also and fetch an exception if you go for try - catch, which I highly recommend 
    } 
}); 
+0

我必須從視圖發送數組到控制器 - 根據你的第一個解決方案? – Skeptic 2013-03-03 14:49:09

+0

兩個解決方案數據都需要發送進行處理,第一個方法使用$ .ajax jquery異步發送,第二個方法加載控制器方法。 – Rastko 2013-03-03 14:58:02

+0

你可以使用ajax方法發佈代碼嗎? – Skeptic 2013-03-04 04:25:44

相關問題