我在一个索引页面中使用了两个表单。
预订表单的验证工作正常,但是当我添加验证以联系我们表单时,仍然验证仅适用于预订表格而非联系我们表格。我已经为表单和全局表单验证文件使用了不同的控制器来验证表单。但是,我仍然无法验证这两种表单。 我也尝试了隐藏字段和$ this->输入 - >帖子('submit')仍然无效验证
下面的代码解释了我的工作。
我已使用路线更改名称。
的index.php
<?php echo form_open('contact',['id'=>'filldetails']); ?>
<div class="field name-box">
<input type="text" id="name" name='name' value="<?php echo set_value('name');?>" placeholder="Who Are You?" required=""/>
<span style='color:red;' class='w3-small'><?php echo"<script>alert('". form_error('name')."')</script>"; ?></span>
<label for="name">Name</label>
<span class="ss-icon">check</span>
</div>
<div class="send wow shake" data-wow-duration="1s" data-wow-delay=".3s">
<input type="hidden" name="Send" value="Send">
<input type="submit" name='Send' value="Send" >
</div>
<?php echo form_close(); ?>
**Different form in same page**
<?php echo form_open('reservation_entries'); ?>
<div class="col-md-12 form-left">
<label><i class="glyphicon glyphicon-calendar"></i>Enter Name:
<span style='color:red;' class='w3-small'><?php echo form_error('name'); ?></span>
</label>
<input type="text" name='name' value="<?php echo set_value('name'); ?>">
</div>
<div class="make wow shake" data-wow-duration="1s" data-wow-delay=".5s">
<input type="hidden" name="Reservation" value="Reservation">
<input type="submit" value="Make a Reservation">
</div>
<?php echo form_close(); ?>
Entry.php控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Entry extends CI_Controller {
public function __construct()
{
Parent::__construct();
$this->load->model('Restaurant');
}
public function index()
{
$this->load->view('index');
}
public function Reservation()
{
if($this->input->post('submit')== 'Make a Reservation')
{
if($this->form_validation->run('Reservation')== false)
{
$this->load->view('index');
}
else
{
#getting values of form
$data['a'] = array(
'Person_Name' => $this->input->post('name')
);
$data['a'] = $this->security->xss_clean($data['a']); #cleaning data
if($this->Restaurant->insert_entries($data['a']))
{
$this->session->set_flashdata('message','Your Reservation has accepted, We will call you back shortly.');
redirect();
}
else
{
$this->session->set_flashdata('message','Some techincal issue occured, your reservation did not complete.');
redirect();
}
}
}#EOF MAIN IF
}
public function contact()
{
if($this->input->post('submit')=='Send')
{
if($this->form_validation->run('Contact')== false)
{
$this->load->view('index');
}
else
{
$data['contact'] = [
'Person_Name' => $this->input->post('name')
];
if($this->Restaurant->contact_entries($data['contact']))
{
$this->session->set_flashdata('message','Thank Your for sharing you message with Black Pepper, We will contact you back shortly.');
redirect();
}
else
{
$this->session->set_flashdata('message','Some techincal issue occured, your query did not complete.');
redirect();
}
}
}#EOF MAIN IF
else
{
echo "sorry";
}
}
form_validation.php 的配置/ form_validation.php
<?php
$config= array(
'Reservation' => array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|max_length[50]|min_length[4]|trim|htmlspecialchars|alpha|xss_clean',
'errors' => ['required'=>'You must provide %s','max_length'=>'Only 50 letters are allowed','min_length'=>'Alteast 4 letters are allowed','alpha'=>'%s contains only alphabet without space.']
)
),
'Contact' => array(
[
'field' => 'name',
'label' => 'Name',
'rules' => 'required|max_length[50]|min_length[4]|trim|htmlspecialchars|alpha|xss_clean',
'errors' => ['required'=>'You must provide %s','max_length'=>'Only 50 letters are allowed','min_length'=>'Alteast 4 letters are allowed','alpha'=>'%s contains only alphabet without space.']
],
) #EOF CONTACT RULES
);
?>