【问题标题】:CodeIgniter form_open not hitting controllerCodeIgniter form_open 没有击中控制器
【发布时间】:2013-06-16 10:05:46
【问题描述】:

大家好,我是 CodeIgniter 框架的新手。我试图开发一个包含表单的页面。

这里是视图


<?php echo form_open('Site/check');?>

User Name: <?php   echo form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " ')); ?> 
Add User:<?php   echo form_submit('submit','Add User','class="btn btn-primary"');?> 
<?php echo  form_close();?>


这是控制器


<?php class Site extends CI_Controller{

  function login() {
      $data['records']="BHOOM!!!";
      $this->load->view('login',$data);
  }

  function check() {
     $this->load->view('showmessage');
  }

}
?>

showmessage 是一个 PHP 页面,在 &lt;h1&gt; 标记中带有 hello。 我已经用谷歌搜索过类似的问题,但没有得到任何解决方案

基本网址是localhost/ci/index.php

错误

无法加载请求的文件:showmessage.php

.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|js|css|img\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

【问题讨论】:

  • 其中也已经有一个 form_close() 。但我无法将其包含在问题中。我使用了 pre 和 code.. ctrl+k 也。
  • 试试:site/check(小写的's')。
  • 显示什么错误?它到底是什么,不起作用?
  • 你把'showmessage'文件,即文件夹结构放在哪里?
  • @nevermind 最好使用base_url(),在此示例中为base_url('site/check'),如果“nice url”已打开。

标签: codeigniter .htaccess routes codeigniter-2


【解决方案1】:

Here 是直接来自EllisLab 的教程。按照步骤操作,您的表单将正常工作。

最常见的错误,

  1. 未加载 form 助手
  2. htaccess. 设置错误,或路径错误(大写字母等)。
  3. config.php 设置不正确
  4. 未将数据传递给视图 ($this-&gt;load-&gt;view('some_view', $data);

因此,如果存在错误消息,您没有向我们提供错误消息,因此社区没有可能的帮助。

如需更多调试信息,请开启 codeigniters profiler ($this-&gt;output-&gt;enable_profiler(TRUE);),使用 PHP 原生函数 var_dump($variable);

确保在您的index.php 中将error reporting 设置为E_ALL

编辑

确保您的/config/config.php

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "";

让你的.htaccess 这个文件应该位于你项目的根目录下,例如。 /ci/.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /ci/

  # Reenable after getting ssl cert
  # RewriteCond %{HTTPS} off
  # RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

  # Removes access to the system folder by users
  RewriteCond %{REQUEST_URI} ^system.*
  RewriteRule ^(.*)$ /index.php?/$1 [L]

  # When your application folder isn't in the system folder This snippet prevents user access to the application folder
  RewriteCond %{REQUEST_URI} ^application.*
  RewriteRule ^(.*)$ /index.php?/$1 [L]

  # Checks to see if the user is attempting to access a valid file, such as an image or css document, if this isn't true it sends the request to index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  # If we don't have mod_rewrite installed, all 404's can be sent to index.php, and everything works as normal.
  ErrorDocument 404 /index.php
</IfModule>

【讨论】:

  • 感谢 Kyslik,我已添加错误和 htaccess 详细信息。回复: 1. helper中添加了form和url。 3. 我必须在配置中设置什么? 4. 我尝试传递一个变量,但也没有成功。
  • 看看ellislab.com/codeigniter/user-guide/general/views.htmlAdding Dynamic Data to the View 部分,这就是如何将变量传递给视图,但恐怕你不需要这个,就问题而言。请向我们提供导致您的问题的正确问题或错误。
  • 谢谢大家,我删除了所有文件并从头开始创建所有内容。我重新启动了 wamp 服务器,现在一切看起来都很好。系统没有正确重定向。
【解决方案2】:

只需使用这种类型的代码进行试用

查看

<?php 
echo form_open('controllet_name/method_name'); 
form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " '));
form_close();
?>

控制器

()
{
    $name=$this->input->post('username');
echo $name ;
    }

【讨论】:

    【解决方案3】:

    我认为你的 RewriteRule 是错误的。 尝试: 重写规则 ^(.*)$ /ci/index.php/$1 [L]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 2016-05-07
      • 2011-09-09
      • 2012-06-14
      • 2015-03-01
      相关资源
      最近更新 更多