我在使用 Codeigniter 验证表单时遇到问题。下面是我在项目中使用的代码。
我已经遵循了我在Codeigniter用户指南中阅读的所有规则,但我不知道这个问题是什么;验证没有发生。
控制器(页.php(
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Page extends Front_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Page_model');
$this->load->helper(array('form', 'url'));
}
public function index()
{
$data['page_title'] = 'Doctors Feedback';
$data['base_url']= $this->uri->segment_array();
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('qualification', 'Qualification', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('residential', 'Residential Address', 'required');
$this->form_validation->set_rules('clinic', 'Clinic Address', 'required');
$this->form_validation->set_rules('email', 'E-mail', 'required');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
$this->form_validation->set_rules('phone', 'Phone Number', 'required');
$this->form_validation->set_rules('comment', 'Comment', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->view('doctors_form', $data);
}
}
}
?>
景观 (doctors_form.php(
<form>
<div class="col-md-12 form-group">
<label>Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo set_value('name')?>" placeholder="Name">
<span class="text-danger"><?php echo form_error("name");?></span>
</div>
<div class="col-md-6 form-group">
<label>Select Gender</label>
<select class="form-control jcf-hidden" data-jcf="{"wrapNative": false, "wrapNativeOnMobile": false}">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<div class="col-md-6 form-group">
<label>Qualification</label>
<input type="text" class="form-control" id="qualification" name="qualification" value="<?php echo set_value('qualification')?>" placeholder="Qualification">
<span class="text-danger"><?php echo form_error("qualification");?></span>
</div>
<div class="col-md-6 form-group">
<label>Age</label>
<input type="text" class="form-control" id="age" name="age" value="<?php echo set_value('age')?>" placeholder="Age">
<span class="text-danger"><?php echo form_error("age");?></span>
</div>
<div class="col-md-6 form-group">
<label>Date</label>
<input type="text" class="form-control" id="date" name="date" value="<?php echo set_value('date')?>" placeholder="Date">
<span class="text-danger"><?php echo form_error("date");?></span>
</div>
<div class="col-md-12 form-group">
<label>Residential Address</label>
<textarea rowa="5" class="form-control" id="address" name="residential" placeholder="Residential Address"></textarea>
<span class="text-danger"><?php echo form_error("residential");?></span>
</div>
<div class="col-md-12 form-group">
<label>Clinic Address</label>
<textarea rowa="5" class="form-control" id="address" name="clinic" placeholder="Clinic Address"></textarea>
<span class="text-danger"><?php echo form_error("clinic");?></span>
</div>
<div class="col-md-12 form-group">
<label>Email</label>
<input type="text" class="form-control" id="email" name="email" value="<?php echo set_value('email')?>" placeholder="Email">
<span class="text-danger"><?php echo form_error("email");?></span>
</div>
<div class="col-md-6 form-group">
<label>Mobile</label>
<input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo set_value('mobile')?>" placeholder="Mobile">
<span class="text-danger"><?php echo form_error("mobile");?></span>
</div>
<div class="col-md-6 form-group">
<label>Phone</label>
<input type="text" class="form-control" id="phone" name="phone" value="<?php echo set_value('phone')?>" placeholder="Phone">
<span class="text-danger"><?php echo form_error("phone");?></span>
</div>
<div class="col-md-12 form-group">
<label>Comment</label>
<textarea rowa="5" class="form-control" id="comment" name="comment" placeholder="Comment"></textarea>
<span class="text-danger"><?php echo form_error("comment");?></span>
</div>
<div class="col-md-12 text-right">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
首先,您应该将控制器名称重命名为 Page.php(首字母大写(。控制器代码应如下所示:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Page extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Page_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
public function index()
{
if ($_POST)
{
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('qualification', 'Qualification', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('residential', 'Residential Address', 'required');
$this->form_validation->set_rules('clinic', 'Clinic Address', 'required');
$this->form_validation->set_rules('email', 'E-mail', 'required');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
$this->form_validation->set_rules('phone', 'Phone Number', 'required');
$this->form_validation->set_rules('comment', 'Comment', 'required');
if ($this->form_validation->run() == FALSE)
{
$data['page_title'] = 'Doctors Feedback';
$data['base_url'] = $this->uri->segment_array();
$this->view('doctors_form', $data);
}
else
{
$name = $this->input->post('name',TRUE);
$qualification = $this->input->post('qualification',TRUE);
$age = $this->input->post('age',TRUE);
$date = $this->input->post('date',TRUE);
$residential = $this->input->post('residential',TRUE);
$clinic = $this->input->post('clinic',TRUE);
$email = $this->input->post('email',TRUE);
$mobile = $this->input->post('mobile',TRUE);
$phone = $this->input->post('phone',TRUE);
$comment = $this->input->post('comment',TRUE);
$data = array(
'name' => $name ,
'qualification' => $qualification ,
'age' => $age ,
'date' => $date ,
'residential' => $residential ,
'clinic' => $clinic ,
'email' => $email ,
'mobile' => $mobile ,
'phone' => $phone ,
'comment' => $comment ,
);
// Further deal with model using this array
}
}
}
}
在您看来,您应该最好使用代码点火器表单打开和关闭标签而不是 HTML 。
<?php echo form_open('Controller_name/function_name'); ?> // equivalent to <form action='url'>
<?php echo form_close(); ?> // equivalent to </form>
在您的情况下,您不必在 form_open 标记中写入函数名称。只需编写控制器名称,因为默认情况下会加载索引函数。