twitter-bootstrap Codeigniter 如何使用 form_open 辅助函数设置表单操作 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30148081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:39:02  来源:igfitidea点击:

Codeigniter how to set form action url using form_open helper function

phphtmltwitter-bootstrapcodeigniter

提问by

I am using Codeigniter with Bootstrap. This is my view code:

我正在使用带有 Bootstrap 的 Codeigniter。这是我的视图代码:

<?php echo form_open('con_login/loginUser'); ?>
    <div class="bs-example">
        <div class="col-md-2">
            <label class="sr-only" for="username">Username</label>
            <input type="text" class="form-control" name="username" placeholder="Username" value="">
        </div>
        <div class="col-md-2">
            <label class="sr-only" for="password">Password</label>
            <input type="password" class="form-control" name="password" placeholder="Password" value="">
        </div>
        <button type="submit" class="btn btn-default" name="login" value="login">Sign in</button>
    </div>
    <?php echo form_close(); ?> 

The code for the called function which is in the controller is:

控制器中被调用函数的代码是:

public function loginUser() {
    $username = $this->mod_login->getUsername();

    $inUser = $this->input->post('username');
    $inPass = $this->input->post('password');

    if (in_array($inUser, $username)) {
        $password = $this->mod_login->getPassword($inUser);
        if ($password === $inPass) {
            $this->load->view('Alt_home');
        }
    }
}

But I get the error

但我得到了错误

The requested URL /SEP/con_login/loginUser was not found on this server.

在此服务器上找不到请求的 URL /SEP/con_login/loginUser。

Why is this? Is there anything to do with Routing?

为什么是这样?和路由有关系吗?

回答by Navneet Singh

You will need to pass complete url to the form_open function like this

您需要像这样将完整的 url 传递给 form_open 函数

<form action='http://localhost/folder/controller/method'>

There are two method to do this

有两种方法可以做到这一点

1: Using base_url()

1:使用 base_url()

Step 1: Open the config.php file under application\config folder

第一步:打开application\config文件夹下的config.php文件

step 2: Set the value of $config['base_url']to your website path

第 2 步:将 的值设置$config['base_url']为您的网站路径

  $config['base_url'] = "http://localhost/foldername/index.php";

How to use

如何使用

Load the url helper in controller

在控制器中加载 url helper

 $this->load->helper('url);

Now in the view file you can use like this

现在在视图文件中,您可以像这样使用

  echo form_open(base_url()."/controllername/mehod");

2: Manually

2:手动

   form_open("http://localhost/foldername/index.php/controller/method");

回答by Shahbaz

You will need to pass complete url to the form_open function like this

您需要像这样将完整的 url 传递给 form_open 函数

<form action='http://localhost/folder/controller/method'>

There are two method to do this

有两种方法可以做到这一点

1: Using base_url()

1:使用 base_url()

Step 1: Open the config.php file under application\config folder

第一步:打开application\config文件夹下的config.php文件

step 2: Set the value of $config['base_url'] to your website path

第 2 步:将 $config['base_url'] 的值设置为您的网站路径

$config['base_url'] = "http://localhost/foldername/index.php";

How to use

如何使用

Load the url helper in controller

在控制器中加载 url helper

$this->load->helper('url);

Now in the view file you can use like this

现在在视图文件中,您可以像这样使用

 echo form_open(base_url()."/controllername/mehod");

2: Manually

2:手动

form_open("http://localhost/foldername/index.php/controller/method");

回答by Varsha

did you load helper (url,form) in your controller if not then u should make a load function in your controller just like this

您是否在控制器中加载了 helper (url,form) 如果没有,那么您应该像这样在控制器中创建一个加载功能

public function load(){  
$this->load->helper('url');
$this->load->helper('form');
}

and use this in function

并在功能中使用它

    public function loginUser() {
    $this->load();
    $username = $this->mod_login->getUsername();

    $inUser = $this->input->post('username');
    $inPass = $this->input->post('password');

    if (in_array($inUser, $username)) {
        $password = $this->mod_login->getPassword($inUser);
        if ($password === $inPass) {
            $this->load->view('Alt_home');
        }
    }
}

回答by Vishal P Gothi

You can use like this, Its works for me.. lets see if it is works for you or not. If you don't want attributes remove from second argument of form_open() function.

你可以这样使用,它对我有用..让我们看看它是否适合你。如果您不想从 form_open() 函数的第二个参数中删除属性。

<?php $attributes = array('class' => 'myformclass', 'id' => 'myformid','name' => 'myformname');
            echo form_open(base_url("con_login/loginUser"),$attributes);?>

HTML HERE

此处为 HTML

<?php echo form_close();?>

Thank You

谢谢