在 Codeigniter 中按下提交按钮后出现 404 错误
After pressing submit button in Codeigniter 404 error appears
我创建了一个联系表单以将详细信息提交到名为 'ccp' 的数据库,table 名称为 'inquiries'
但是按下提交按钮后,并没有跳转到任何页面,也没有向数据库提交数据。请帮我解决这个错误。
控制器文件 - Contactform.php
<?php
class Contactform extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session','form_validation'));
$this->load->database();
}
function index(){
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('Contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);
if ($this->db->insert('inquiries', $data))
{
// success
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('Contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('Contactform/index');
}
}
}
//custom callback to accept only alphabets and space input
function alpha_space_only($str){
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
查看文件 - Contact_form_view.php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ciao Code Contact Form</title>
<link href="<?php echo base_url("css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Contact Form</h3>
</div>
<div class="panel-body">
<?php $attributes = array("name" => "Contactform");
echo form_open("Contactform/index", $attributes);?>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-success">Submit</button>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
</body>
</html>
除此之外,database.php文件更新如下
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ccp',
'dbdriver' => 'mysqli',
和routes.php如下:
$route['default_controller'] = 'Contactform';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
下面是我遇到的错误
遵循这些方法
- 为您的项目设置基础 URL。
- 在图像中,我可以看到类似
[::1]
的内容。它的克服是因为每当 base URL 为空时。
加载没有 index.php
的网站。
你可以在 form open 中添加 index.php
来避免这种情况。但不推荐。
echo form_open("index.php/Contactform/index", $attributes);?>
我创建了一个联系表单以将详细信息提交到名为 'ccp' 的数据库,table 名称为 'inquiries' 但是按下提交按钮后,并没有跳转到任何页面,也没有向数据库提交数据。请帮我解决这个错误。
控制器文件 - Contactform.php
<?php
class Contactform extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session','form_validation'));
$this->load->database();
}
function index(){
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('Contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);
if ($this->db->insert('inquiries', $data))
{
// success
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('Contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('Contactform/index');
}
}
}
//custom callback to accept only alphabets and space input
function alpha_space_only($str){
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
查看文件 - Contact_form_view.php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ciao Code Contact Form</title>
<link href="<?php echo base_url("css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Contact Form</h3>
</div>
<div class="panel-body">
<?php $attributes = array("name" => "Contactform");
echo form_open("Contactform/index", $attributes);?>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-success">Submit</button>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
</body>
</html>
除此之外,database.php文件更新如下
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ccp',
'dbdriver' => 'mysqli',
和routes.php如下:
$route['default_controller'] = 'Contactform';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
下面是我遇到的错误
遵循这些方法
- 为您的项目设置基础 URL。
- 在图像中,我可以看到类似
[::1]
的内容。它的克服是因为每当 base URL 为空时。
- 在图像中,我可以看到类似
加载没有
index.php
的网站。你可以在 form open 中添加
index.php
来避免这种情况。但不推荐。echo form_open("index.php/Contactform/index", $attributes);?>