【问题标题】:codeigniter form not workingcodeigniter 表单不起作用
【发布时间】:2014-07-22 20:59:51
【问题描述】:

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

请注意下面我的代码。

查看(站点标题)

<?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>

模型(模型搜索)

<?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();

    }
}
?>

控制器(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>

【问题讨论】:

    标签: php .htaccess codeigniter web


    【解决方案1】:

    也许是个愚蠢的问题,但是您是否从 config.php 中的 $config['index_page'] 变量中删除了“index.php”?

    如果您不使用任何路由将 'search_keyword' 链接到 'site/search_keyword',请务必使用之前指定的 form_open('site/search_keyword')。

    顺便说一句,这是我在 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]
    

    【讨论】:

    • 我确实从配置文件中删除了 index.php,我会试试这个新的 htaccess,谢谢
    • new .htaccess 似乎也不起作用。有人有其他建议吗?
    【解决方案2】:

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

    form_open('site/search_keyword')
    

    【讨论】:

    • 即使按照建议添加控制器名称,我也会收到相同的错误并且找不到页面
    • 它试图访问哪个 URL?您在网址栏中看到了什么?
    • 我收到此错误,请求的 URL /stumble/site/search_keyword 在此服务器上未找到。它托管在 WAMP 上,仅供参考
    • 搜索页面的网址是什么?您的base_url 配置选项设置正确吗?
    • base_url 设置正确,因为样式表被引用。搜索页面的 URL 现在只是索引页面,因为表单将出现在每个页面的标题中。它位于 localhost/stumble/index.php
    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多