codeigniter形式不起作用

时间:2014-06-02 16:58:52

标签: php .htaccess codeigniter web

我正在尝试在我的codeigniter网站标题中创建一个搜索表单,但是每次提交表单时,都会收到404错误,说明无法找到该页面!我试图创建一个测试页面的链接,这给了我同样的错误。

请注意下面的代码。

view(site_header)

<?php echo doctype(); ?>
<html lang="en">
    <link href="<?php echo base_url(); ?>styles/style.css" type="text/css" rel="stylesheet"/>
    <head>

        <title>/title>
        <div id="container">
            <div id="search">
                <?php

                 echo form_open('search_keyword');
                 echo form_label("Stumble a search ", "searchfor");
                 echo form_input("search","search");
                 echo form_submit("getSearch","Search");
                 echo form_close(); ?>
            </div>

        </div>
    </head>
</html>

model(model_search)

<?php
class Model_search extends CI_Model {

    public function get_results($search_term){
       $query = $this->db->query('SELECT embed, title FROM videos WHERE tags LIKE '%$search_term%' order by RAND() LIMIT  1');
      return $query->result();

    }
}
?>

Controller(site.php)默认控制器

<?php

 if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Site extends CI_Controller {
    public function index(){
        $this->home();
    }

    public function home(){ 
        $this->load->model("model_get");
        $data["results"] = $this->model_get->getRand();
        $this->load->view("site_header");
        $this->load->view("site_content", $data);
        $this->load->view("site_footer");
    }      
    public function search_keyword()
    {
        $this->load->model('model_search');
        $search_term  =  $this->input->post('search');
        $data['results'] = $this->model_search->get_results($search_term);
        $this->load->view('site_header');
        $this->load->view('search_content',$data);
        $this->load->view('site_footer');
    }
}

?>

结果页面(search_content)

 <body>
     <link href="<?php echo base_url(); ?>styles/style.css" type="text/css" rel="stylesheet"/>
     <div id="container">
         <div id="intro">
            <?php echo heading("Search Results",1);?>   
         </div> 
         <div id ="content">
             <p>Stumble videos related to <?php echo $search_term; ?> </p>
             <?php
             foreach ($results as $row) {
                 $title = $row->title;
                 $vid = $row->embed; 
             }
             echo heading($title, 3);
             echo $vid;
             ?>

        </div>
    </div>
</body>

也许我错过了一些明显的东西,但我认为这可能与我的.htaccess文件有关,该文件发布在下面

(。htaccess的)

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

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>

2 个答案:

答案 0 :(得分:1)

CodeIgniter URL需要 控制器名称和方法。

form_open('site/search_keyword')

答案 1 :(得分:0)

也许是一个愚蠢的问题,但是你删除了&#34; index.php&#34;来自config.php中的$ config [&#39; index_page&#39;]变量?

如果您没有使用任何路线链接&#39; search_keyword&#39;要访问&#39; site / search_keyword&#39;,请务必使用之前指定的form_open(&#39; site / search_keyword&#39;)。

顺便说一句,这里是我总是在我的Codeigniter项目上用来重写网址的.htaccess。只需在此处添加RewriteBase即可使其正常工作。

DirectoryIndex index.php

RewriteEngine on
RewriteCond $1 !^(index\.php|img|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]