表格打开重定向到Codeigniter中的404

问题描述 投票:0回答:3

我正在撰写一份表格。我已经创建了一个Controller“Search”,在那里我有“doSearch()”函数,我加载了一个视图。

<?php echo form_open('Search/doSearch'); ?>

单击提交按钮时,我需要在“搜索”控制器中调用该方法。

当我点击表单提交按钮时,我的请求是直接进入

http://localhost/mysite/index.php/Search/doSearch页面给出404错误。怎么解决这个问题。

码:

第一个查看我写表单的地方

<html>
<body lang="en-US" dir="ltr">

<?php echo form_open('Search/doSearch'); ?>
    <center
    <table>
        <tr>
            <td>
                 <input type="text" name="item"/>
            </td>
              <td>
                In
            </td>
              <td>
                 <input type="text" name="location"/>
            </td>
        </tr>
        <tr>
            <td>
               <input type="submit" value="Search">
                </td>
                </tr>
        </table>
</center>
 <br>
<?php echo form_close(); ?>
</body>
</html>

预期的控制器处理表单提交的请求

<?php
class Search extends CI_Controller {

    public function doSearch()
    {
        $this->load->view('search_page');

    }
}

我在视图中写过“serch_page”视图。一旦我点击搜索按钮,它就会触发到http://localhost/mysite/index.phmysite/Search/doSearch然后给我

404 Page Not Found
The page you requested was not found.

application / .htaccess文件

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

routes.php文件

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

编辑:我在route.php中设置条目并检查每个其他位置。现在,一旦我点击表单提交,我在http://localhost/mysite/Search/doSearch页面中出现以下错误

Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404
localhost
php codeigniter
3个回答
0
投票

我相信问题是你需要在使用斜杠调用form_helper时启动动作URL。而不是这个:

<?php echo form_open('Search/doSearch'); ?>

试试这个:

<?php echo form_open('/Search/doSearch'); ?>

我相信问题在于,你提供的任何动作网址都不是以斜线开头,它将被解释为relative url,所以如果你要显示这个形式的其他控制器 - 比如网址是http://localhost/mysite/index.php/controller/method - 那么你的部分亲属url Search / doSearch最终会提交给http://localhost/mysite/index.php/controller/methodSearch/doSearch

注意:我的答案假设您的Search.php位于您的控制器目录的根目录而不是某个子目录。如果它在某个子目录中,比如application / controllers / subdir / Search.php,那么你应该这样做:

<?php echo form_open('/subdir/Search/doSearch'); ?>

编辑:仔细查看您的帖子,看起来您的CodeIgniter项目由于某种原因存在于子目录mysite中。既然如此,试试这个:

<?php echo form_open('/mysite/Search/doSearch'); ?>

0
投票

“我在视图中写了”serch_page“视图。一旦我点击搜索按钮”

这里的视图页面名称是“serch_page”,在doSearch函数中,视图页面名称是'search_page',请注意不匹配..您必须将您的视图页面名称重命名为search_page.php而不是serch_page.php


0
投票

我认为这是因为您使用的上/下字符试试这个,它应该工作:

<?php echo form_open('search/dosearch'); ?>
© www.soinside.com 2019 - 2024. All rights reserved.