简体   繁体   English

Codeigniter中的Form_open无法正常工作

[英]Form_open in codeigniter not working

I've made a simple project in which a user can register his account, login with his credentials, view his blog posts and create a new blog. 我做了一个简单的项目,用户可以在其中注册他的帐户,使用他的凭据登录,查看他的博客文章并创建一个新博客。 I'm currently working on creating a new blog which is simple a form with two options: blog title and content. 我目前正在创建一个新的博客,它是一种简单的表单,具有两个选项:博客标题和内容。 I'm stuck with the part where you hit submit and the form is passed to the controller specified in form_open function. 我停留在您点击提交的部分,并将表单传递到form_open函数中指定的控制器。 Nothing is happening when I hit the submit button, this is the issue. 当我按下“提交”按钮时,什么都没有发生,这就是问题所在。

Here is my view file: 这是我的视图文件:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
        <meta charset="utf-8">
        <title>Enter your Title</title>
    </head>
    <body>
        <div class="container">
            <div class="jumbotron">
                <?=form_open("blog/submitpost"); ?>
                <div class="form-horizontal">
                    <div class="form-group">
                        <label for="blogtitle" class="control-label col-sm-2">Blog Title</label>
                        <div class="col-sm-8">
                            <input type="text" name="blogtitle" placeholder="Enter your title here" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="blogdesc" class="control-label col-sm-2">Blog Title</label>

                        <div class="col-sm-8">
                            <textarea name="blogdesc" rows="8" cols="40" class="form-control" placeholder="Enter your post here"></textarea>
                        </div>
                    </div>
                </div>
            <?=form_close();?>
            <div style="text-align: center;">
                <button type="submit" name="submit" class="btn btn-success">Submit</button>
            </div>
            </div>
        </div>

    </body>
</html>

And here is my controller 这是我的控制器

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

class Blog extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->model('user');
        $this->load->helper('form');
    }


    public function new_post($id){
        $data['id'] = $id;
        $this->load->view('blog_new_post',$data);
    }

    public function blogpostview($id){
        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];
        $data['id'] = $id;
        $data['posts'] = $this->user->blog_post_list($data['username']);
        $this->load->view('blog_view',$data);
    }

    public function submitpost(){
      //there will be additional code here
        $this->load->view('blog_post_view');
    }

}
?>

Now firstly, I'm aware one has to load the Form helper class to achieve this, which I've already done. 现在,首先,我知道必须加载Form helper类来实现此目的,而我已经完成了。 I've even changed the autoload.php file to automatically load it. 我什至更改了autoload.php文件以自动加载它。

Secondly, I've already implemented this twice during registration and login processes. 其次,我已经在注册和登录过程中实现了两次。 This time however I don't know why the code isn't working. 但是这次我不知道为什么代码不起作用。

Okay I got the solution and it's a very simple one. 好的,我得到了解决方案,这是一个非常简单的解决方案。 I had not placed the submit button in the form due to which the problem was coming! 由于问题来临,我没有将提交按钮放在表单中! Sometimes it's the simplest solutions that work out. 有时,这是最简单的解决方案。

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
        <meta charset="utf-8">
        <title>Enter your Title</title>
    </head>
    <body>
        <div class="container">
            <div class="jumbotron">
                <?php echo form_open("blog/submitpost"); ?>
                <div class="form-horizontal">
                    <div class="form-group">
                        <label for="blogtitle" class="control-label col-sm-2">Blog Title</label>
                        <div class="col-sm-8">
                            <input type="text" name="blogtitle" placeholder="Enter your title here" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="blogdesc" class="control-label col-sm-2">Blog Title</label>

                        <div class="col-sm-8">
                            <textarea name="blogdesc" rows="8" cols="40" class="form-control" placeholder="Enter your post here"></textarea>
                        </div>
                    </div>
                </div>
            <div style="text-align: center;">
                <button type="submit" name="submit" class="btn btn-success">Submit</button>
            </div>
            <?=form_close();?>
            </div>
        </div>

    </body>
</html>

try this 尝试这个

<?php 
 $attributes = array('name' => 'add_blog','id' => 'add_blog', 'method' =>'POST')
 echo form_open("blog/submitpost", $attributes); ?> //form open


    <?php echo form_close(); ?> //form close

Try this..... 尝试这个.....

    <?php 


     $data = array('action' =>'blog/submitpost','id' => 'add_blog', 'method' =>'POST', )
     echo form_open("blog/submitpost", $data); ?> 
    <div class="form-horizontal">
                    <div class="form-group">
                        <label for="blogtitle" class="control-label col-sm-2">Blog Title</label>
                        <div class="col-sm-8">
                            <input type="text" name="blogtitle" placeholder="Enter your title here" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="blogdesc" class="control-label col-sm-2">Blog Title</label>

                        <div class="col-sm-8">
                            <textarea name="blogdesc" rows="8" cols="40" class="form-control" placeholder="Enter your post here"></textarea>
                        </div>
                    </div>
                </div>
            <div style="text-align: center;">
                <button type="submit" name="submit" class="btn btn-success">Submit</button>
            </div>
 <?php echo form_close(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM