2011-07-22 38 views
5

我嘗試了一個簡單的表單提交但我無法使用$this->input->post以及$_POST[]方法獲取控制器上的表單值。我的看法部分是無法獲得帖子在codeigniter中提交值

<html> 
<head> 
    <title> Feedback page</title>  
</head> 
<body> 

    <?php echo form_open('feedback/save'); ?>  
    <p> 
     <label>name: </label> 
     <?php echo form_input('name'); ?> 

    </p> 
    <p> 
    <label>Email: </label> 
     <?php echo form_input('email'); ?> 
    </p> 
    <p> 
    <label>Feedback: </label> 
     <?php echo form_textarea('feedback'); ?> 
    </p> 
    <p> 
     <?php echo form_submit('submit','Submit'); ?> 
    </p> 

    <?php echo form_close(); ?> 

</body> 

</html> 

和控制器部分是

<?php 
class Feedback extends CI_Controller { 

function __construct() { 
    parent::__construct();  
    $this->load->model("MFeedback"); 

} 
function index() { 

    $this->load->view('home/feedback_view.php'); 
    //print "loaded"; 


} 

function save() { 
    print "called";  
    print_r($this->input); 
    $name = $this->input->post('uname'); 
    $email = $this->input->post('email'); 
    $feedback = $this->input->post('feedback'); 
    print $name . $email . $feedback; 
    $this->index(); 
} 

} 
?> 

我不知道哪裏出了問題這裏或在那裏我需要看它的任何配置設置。?

回答

6

我發現了這個問題。它實際上是用重寫規則。請確保在codeigniter的根文件夾上有

RewriteEngine On 
RewriteRule ^(application) - [F,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* index.php/$0 [PT,L] 

重寫規則。

+0

?或在'/'?? – Prakash

0

首先,在您的視圖中,您已將您的一個輸入的名稱指定爲name,在您查找的控制器中爲uname

其次,我不記得是否笨不相同,以$_POST但它肯定破壞$_GET陣列。如果你想要的一切後輸入一個關聯數組,你可以調用這個:

$this->input->post(); 

第三,在你的輸入可能會得到阻止XSS Filtering一個非常非常罕見的情況下,可以從發生通過調用它像這樣停止這種(僅用於檢查的目的,看看有什麼是錯的,不要在生產中使用這個)

$this->input->post(NULL, FALSE); 

如果事情一般是錯誤的,這些調用將返回FALSE,您應該測試此使用===運營商,因爲它只會匹配FALSE其中==也將匹配NULL。第四,您應該使用簡單的html表單快速測試,看起來您正在用表單幫助程序來構建表單,但使用簡單的HTML表單進行快速測試絕不會讓人感覺不舒服。

除此之外,你需要提供更多關於你的環境/配置/生成的html/etc的信息......供我們弄清楚。你真的沒有給我們很多工作。

+0

呃傻我,我沒有發現uname /名稱問題:) – conor

+0

實際上沒有任何的伎倆工作。我可以看到$ _POST爲空,所以不管uname還是name都沒關係,我修正了它。 –

+0

CI_Input對象([ip_address] => [user_agent] => Mozilla/5.0(X11; Linux i686; rv:5.0)Gecko/20100101 Firefox/5.0 [_allow_get_array] => 1 [_enandardize_newlines] => 1 [_enable_xss] => 1 [_enable_csrf] => [headers:protected] => Array()[security] => CI_Security Object([_xss_hash:protected] => ea1f1a23ff5349cfe8470d3ff9cc382e [_csrf_hash:protected] => 7108f2252e604139ed5b64c99959adb8 [_csrf_expire:protected] => 7200 [_csrf_token_name數組([document.cookie] => [已移除] [document.write] => [已移除] [.parentNode] =>數組= [> ci_csrf_token [_csrf_cookie_name:protected] => ci_csrf_token [_never_allowed_str:protected] –

4

看看這個: http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules

我也有類似的問題,當我開始使用CI。您需要爲表單設置至少一個驗證規則,然後檢查提交的表單是否符合該規則。然後,您可以訪問提交的表單數據,如你在上面幹什麼..

這已經有一段時間,因爲我用CI,但這樣的事情應該解決您的問題: (從上面的鏈接獲取)

$this->load->library('form_validation'); 

    $this->form_validation->set_rules('uname', 'Username', 'required'); 
    $this->form_validation->set_rules('email', 'Password', 'required'); 
    $this->form_validation->set_rules('feedback', 'Feedback', 'required'); 
    $this->form_validation->set_rules('email', 'Email', 'required'); 

    if ($this->form_validation->run() == FALSE) 
    { 
        // Here is where you do stuff when the submitted form is invalid. 
     $this->load->view('myform'); 
    } 
    else 
    { 
        // Here is where you do stuff when the submitted form is valid. 
      print "called";  
      print_r($this->input); 
      $name = $this->input->post('uname'); 
      $email = $this->input->post('email'); 
      $feedback = $this->input->post('feedback'); 
      print $name . $email . $feedback; 
      $this->index(); 

    } 

希望能幫助你.. :)

+0

個人意見:CI的表格驗證比它的價值更麻煩。我個人使用我自己的框架,CI方式最終違反了** DRY **,即使有很好的規劃。儘管如此,它可能會解決他的問題:p – Aren

+0

呃幹什麼? ;) – conor

+1

不要在'/ application'中重複自己 – Aren

1

由於過去的半小時無法工作,我還是遇到了同樣的問題。我試過你的解決方案,但沒有幫助。但你說得對,它與路由有關。

我駛過其他變量,以我的行動,如:

domain/controller/action/value1/value2 

,當我有我的表單提交的數據:

domain/index.php/controller/action/value1/value2 

它解決了這個問題。我猜如果你在最後傳遞值,post變量不能按預期工作。我知道它應該工作,我想它也一樣。想想正確設置.htaccess是否有問題。

1

感謝我解決我的問題。我有同樣的問題。我的代碼在WAMP中運行良好,但是當我轉移到LAMP時,我遇到了以前從未見過的各種有線問題,而且沒有獲得任何形式的後期價值是其中之一。

根據上述建議:

我用form_open(index.php/controller/method)代替form_open(controller/method)和它的工作,立竿見影。

但是我刪除了我的index.php,它也沒有顯示在地址欄中。正如我說的有線...

1

使用form action='domain/index.php?/controller/function name/parameter1/parameter2'

例如您的域名是abc.com,控制器是get,函數名value和參數功能ab 然後只寫形式傳遞採取如下行動。

<form action='http:/abc.com/index.php?/get/value/a/b' method='post' > 

我以這種方式解決了我的問題。希望它能爲你工作。 感謝

3

你的URL地址應該是相同config->config.php->$config['base_url'] 如果像

http://www.test.com

你的URL地址,然後你configh應該是

$config['base_url'] = 'http://www.test.com/'; 
0

嗯,我所面臨的同樣的問題和下面的.htaccess的補充幫助解決了我的專業blem。

<Limit GET POST> 
order deny,allow 
deny from all 
allow from all 
</Limit> 
<Limit PUT DELETE> 
order deny,allow 
deny from all 
</Limit> 
0
$data = array('id' => 'email', 
'name' => 'email', 
'class' => 'form-control'); 

echo form_input($data); 

只是一個快速提的是,如果你使用一個數組來設置你的投入等。不要忘了,包括名稱=>您的陣列中的「your_desired_post_variable_name」,因爲這是我的錯,我是給它只是一個ID和想知道爲什麼我的POST數組是空的!不要這樣做! ;)