我做了一个简单的项目,用户可以注册他的帐户,使用他的凭据登录,查看他的博客文章并创建一个新的博客。我目前正在创建一个新博客,这个简单的表单有两个选项:博客标题和内容。我遇到了你点击提交的部分,表单被传递给form_open函数中指定的控制器。点击提交按钮时没有发生任何事情,这就是问题所在。
这是我的观点文件:
<!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>
这是我的控制器
<?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');
}
}
?>
现在首先,我知道必须加载Form帮助程序类来实现这一点,我已经完成了。我甚至更改了autoload.php文件以自动加载它。
其次,我已经在注册和登录过程中实现了两次。但是这次我不知道为什么代码不起作用。
答案 0 :(得分:0)
好的,我得到了解决方案,这是一个非常简单的解决方案。我没有在表单中放置提交按钮,因为问题来了!有时它是最简单的解决方案。
<!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>
答案 1 :(得分:0)
试试这个
<?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
答案 2 :(得分:0)
试试这个......
<?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(); ?>