2012-01-29 65 views
0

我使用jQuery叫我「志願」笨控制器從一個名爲「編輯」觀點,即只有一個參數的方法,「ID」如何從發出Ajax請求的CodeIgniter視圖獲取URI段?

視圖的URI是: volunteer/edit/3

使我的update()方法的調用就像這樣:

$.post('volunteer/update', function(data) { 
    console.log(data); 
});     

所有方法確實現在的問題是呼應URI段:

public function update(){ 
    echo $this->uri->segment(2, 0); 
} 

想要的是調用update()的視圖的URI段(「編輯」視圖)。相反,我得到update()的一段URI。如何獲取編輯視圖URI的一部分,以便我可以在update()方法中使用它?

回答

0

您需要傳入AJAX調用的POST數據中的段值。

的選項有:

1)你可以解析URL W/JavaScript來確定哪些段的值,例如:

<script> 
var postData; 
// assign whatever other data you're passing in 
postData.lastSegment = window.location.href.substr(window.location.href.lastIndexOf('/') + 1); 
$.post('volunteer/update', postData, function(data) { 
    console.log(data); 
}); 
</script> 

2)您可以使用PHP預-populate視圖中的JavaScript變量,然後可以在使用後

<script> 
var postData; 
// assign whatever other data you're passing in 
postData.lastSegment = <?php echo $this->uri->segment(2, 0); ?>; 
$.post('volunteer/update', postData, function(data) { 
    console.log(data); 
}); 
</script> 

終於在update()控制器,你可以拉出來的數據從POST才子h $this->input->post()或出$_POST

+0

最後我只使用一個隱藏輸入更多的細節,但它基本上是做後臺程序的第二個建議是一回事。 – Keyslinger 2012-01-30 21:59:04

2

您可以通過使用

$_SERVER['HTTP_REFERER'] 

獲取引薦URL和比手動解析它,讓您的細分。

它不是100%安全的,因爲它可以被覆蓋(它被服務器設置爲頭)。

硒對這位前輩post