我正试图使用codingsiter中的post方法提交我的表单,当我提交表单时,会从主视图调用我的表单。404找不到页面。
这是我的头表:
<form class="form-horizontal" id="sendform" role="form" action="<?php echo `base_url('/pages/insert_into_db') ;?>" method="post" >`
在控制器中,我有这个功能控制器:
pages.hp:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function view ($page='gurantee'){
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$this->load->view('pages/'.$page);
}
public function index ()
{
$this->load->helper('url');
$this->load->view('Welome');
}
public function display () {
$this->load->model('ektreemodel');
$data['query']=$this->ektreemodel->display();
$this->load->view('table',$data);
}
public function insert_to_db()
{
$this->load->helper('url');
$this->load->model('ektreemodel');
$this->ektreemodel->insert_into_db();
$this->load->view('gurantee');
}
public function dis () {
$this->load->helper('url');
$this->load->view('registration');
}
}
url为:
http://localhost:8888/index.php/pages/index.php/pages/display
当我提交我的表格时,它运行得很好——主要的第一视图,我有什么需要更改的吗?或者,有没有什么方法可以使用javascript,所以当我使用post方法时,它会从控制器调用display函数?
试试这个
<form class="form-horizontal" id="sendform" role="form" action="<?php echo base_url().'/pages/display' ;?>" method="post" >
要使用base_url((,必须首先加载url帮助程序。
$this->load->helper('url');
还要确保在application/config/config.php中提供了站点的基本url。
解决方案1
<form class="form-horizontal" id="sendform" role="form" action="<?php echo site_url('pages/display') ;?>" method="post" >`
使用具有site_url()
而非base_url()
的代码
解决方案2
如果这不起作用,一种更合乎逻辑的方式是:
echo form_open('pages/display', 'class="form-horizontal" id="sendform"');
这将为您生成表单标记。希望这有帮助:(
解决方案#3:
使pages.php
中的"p"资本成为Pages.php
问题是因为我从另一个视图调用视图,所以我不必使用base_ur,因为它将调用localhost/index.php/controller.
在我的例子中,我所在的位置已经从第一个视图调用了我的url。
所以我需要做的就是将表单方法帖子改为:
"method="post">
它将直接调用的位置:http://localhost:8888/index.php/pages/insert_to_db
其中http://localhost:8888/index.php/pages/它以前已经调用过了,所以需要从一开始就调用baseurl。