【问题标题】:Code Igniter: Form's action cannot direct to the right pathCodeigniter:表单操作无法指向正确的路径
【发布时间】:2013-06-19 10:25:36
【问题描述】:

我正在从事 Code Igniter 项目。

我在视图文件夹C:\xampp\htdocs\MSPN\APPLICATION\views中有一个php文件

表格是这样写的:

<form action="<?php echo base_url() .'homeSignUp/main'; ?>" method="post" >

在浏览器中,文件位于localhost/mspn/index.php/signup/main

在 config.php 我有:

$config['base_url'] = 'localhost/mspn/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

“action”表单旨在指向controller 文件夹中的homeSignUp.php,该文件夹具有main 方法。

但是当我尝试运行它时,submit 按钮指向localhost/mspn/homeSignUp/main。 它说:找不到对象!

我一直在尝试更改actionattribute 中的网址,但它一直给我这个错误。 我错过了什么? 谢谢。

【问题讨论】:

    标签: php forms codeigniter path


    【解决方案1】:

    首先,将index_page 配置更改为:

    $config['index_page'] = 'index.php';
    

    然后使用site_url() 喜欢:

    <form action="<?php echo site_url('homeSignUp/main'); ?>" method="post" >
    

    注意:由于您似乎使用的是特定的route,因此您可能希望使用signup/main 而不是homeSignUp/main

    因为站点 URL 将同时包含 base_urlindex_url(如果有)。所以你不需要单独提及index.php

    【讨论】:

    • 我试过这样做,但还是不行。我已经使用了大约 5 个小时,它一直给我“找不到对象!”。我该怎么办?
    • 你能告诉我们homeSignUp控制器的主要功能吗?"
    • 是的,但它有点长。我可以将它发布到这里的答案中还是应该将文件上传到某个地方?
    • 我的意思是说你的函数可能有问题
    • 谢谢! >
    【解决方案2】:

    首先使用.htaccess从您的网址中删除index.php,然后您的网址应如下所示

    localhost/mspn/signup/main
    
    //Remove index.php
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # Removes access to the system folder by users.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    # 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>
    

    你的config.php应该有这个配置

    $config['base_url'] = '';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    $config['url_suffix'] = '';
    

    那么你的表单应该是这样的

    echo form_open(base_url()."homeSignUp/main");
    

    希望有意义

    【讨论】:

    • 我一直试图从路径中删除那个 index.php。但我一直失败。现在它还在那里..
    猜你喜欢
    • 2016-09-26
    • 2013-04-29
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2015-12-16
    相关资源
    最近更新 更多