我已创建了一个注册表单,但每当我尝试将数据插入数据库时,它都不会给我错误,而是它与填充的数据保持在同一页面上。我在codeigniter中创建了这个.Below是我的代码。
控制器文件:Home.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session','form_validation','email'));
$this->load->database();
$this->load->model('Sign');
}
public function index()
{
$this->load->view('home/index');
}
function signup()
{
$occu = $this->input->post('occupation');
$cf = $this->input->post('codefest');
$this->form_validation->set_rules('fname','Name','trim|required');
$this->form_validation->set_rules('mobile','Mobile No','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('password','Password','trim|required');
$this->form_validation->set_rules('cpass','Cofirm Password','trim|required|matches[password]');
$this->form_validation->set_rules('occupation','Occupation','trim|required|callback_select_validate');
$this->form_validation->set_rules('institute','Institute Name','trim|required');
$this->form_validation->set_rules('company','Company Name','trim|required');
$this->form_validation->set_rules('codefest','Codefest','trim|required|callback_select_codefest');
$this->form_validation->set_rules('address1','Address','trim|required');
$this->form_validation->set_rules('city','City','trim|required');
$this->form_validation->set_rules('pincode','Pincode','trim|required');
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('home/index');
}
else
{
//echo "inserted";
$data = array
('user_name' => $this->input->post('fname'),
'mobile_no' => $this->input->post('mobile'),
'user_email' => $this->input->post('email'),
'user_password' => $this->input->post('cpass'),
'occupation' => $this->input->post('occupation'),
'institute_name' => $this->input->post('institute'),
'company_name' => $this->input->post('company'),
'codefest_city' => $this->input->post('codefest'),
'address' => $this->input->post('address1'),
'city' => $this->input->post('city'),
'pincode' => $this->input->post('pincode'));
// insert form data into database
if ($this->Sign->insertUser($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your Message has been successfully received.We will back to you soon.</div>');
redirect('home/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('home/index');
}
}
}
// Below function is called for validating select option field.
function select_validate($occu)
{
if($occu=="none"){
$this->form_validation->set_message('select_validate', 'Please Select Your Occupation.');
return false;
} else{
// User picked something.
return true;
}
}
function select_codefest($cf)
{
if($cf=="none"){
$this->form_validation->set_message('select_codefest', 'Please Select a Codefest.');
return false;
} else{
// User picked something.
return true;
}
}
}
/* End of file Home.php */
/* Location: .//C/Users/CYBERBUFF/AppData/Local/Temp/fz3temp-1/Home.php */
模型文件:Sign.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sign extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function insertUsers($data)
{
return $this->db->insert('signup',$data);
}
}
/* End of file Sign.php */
/* Location: .//C/Users/CYBERBUFF/AppData/Local/Temp/fz3temp-1/Sign.php */
查看文件:index.php
<div class="second-col col-md-6 col-sm-3">
<?php echo $this->session->flashdata('msg'); ?>
<div class="contact-area-left">
<h4>Sign Up</h4>
<?php echo form_open('home/signup', array('class' =>'comments-form contact-form'));?>
<div class="row" align="center">
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="fname" placeholder="Full Name" value="<?php echo set_value('fname');?>">
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="mobile" placeholder="Your Mobile No" value="<?php echo set_value('mobile');?>">
<span class="text-danger"><?php echo form_error('mobile'); ?></span>
</div>
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="email" placeholder="Email Address" value="<?php echo set_value('email');?>">
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group col-lg-5">
<input type="password" class="form-control" name="password" placeholder="Password" value="<?php echo set_value('password');?>">
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
<div class="form-group col-lg-5">
<input type="password" class="form-control" name="cpass" placeholder="Confirm Password" value="<?php echo set_value('cpass');?>">
<span class="text-danger"><?php echo form_error('cpass'); ?></span>
</div>
<div class="form-group col-lg-10">
<select class="form-control" name="occupation" id="occupation" onchange="CheckContact(this.value);" >
<option value="none" selected="selected">------------Select Occupation------------</option>
<option value="student">Student</option>
<option value="professional">Professional</option>
</select>
<span class="text-danger"><?php echo form_error('occupation'); ?></span>
</div>
<div class="form-group col-lg-10" id="student" style="display: none;">
<input type="text" class="form-control" name="institute" placeholder="Institute Name" value="<?php echo set_value('institute');?>">
<span class="text-danger"><?php echo form_error('institute'); ?></span>
</div>
<div class="form-group col-lg-10" id="professional" style="display: none;">
<input type="text" class="form-control" name="company" placeholder="Company Name" value="<?php echo set_value('company');?>">
<span class="text-danger"><?php echo form_error('company'); ?></span>
</div>
<div class="form-group col-lg-10">
<select class="form-control" name="codefest">
<option value="none" selected="selected">------------Select Codefest------------</option>
<option value="cf_ agartala">Codefest Agartala</option>
<option value="cf_bhubaneswar">Codefest Bhubaneswar</option>
<option value="cf_chennai">Codefest Chennai</option>
<option value="cf_guwahati">Codefest Guwahati</option>
<option value="cf_kolkata">Codefest Kolkata</option>
</select>
<span class="text-danger"><?php echo form_error('codefest'); ?></span>
</div>
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="address1" placeholder="Address" value="<?php echo set_value('address1');?>">
<span class="text-danger"><?php echo form_error('address1'); ?></span>
</div>
<!--<div class="form-group col-lg-5">
<input type="text" class="form-control" name="address2" placeholder="Address Line 2 (optional)" value="<?php echo set_value('address2');?>">
<span class="text-danger"><?php echo form_error('address2'); ?></span>
</div>-->
<div class="form-group col-lg-5">
<input type="text" class="form-control" name="city" placeholder="City" value="<?php echo set_value('city');?>">
<span class="text-danger"><?php echo form_error('city'); ?></span>
</div>
<div class="form-group col-lg-5">
<input type="text" class="form-control" name="pincode" placeholder="PIN Code" value="<?php echo set_value('pincode');?>">
<span class="text-danger"><?php echo form_error('pincode'); ?></span>
</div>
</div>
<div class="row" align="center">
<button class="btn btn-primary btn-lg">Register</button>
<button type="reset" class="btn btn-default btn-lg">Reset</button>
</div>
<?php echo form_close(); ?>
</div>
答案 0 :(得分:1)
这些必填字段隐藏在表单中,因此验证失败;
<div class="form-group col-lg-10" id="student" style="display: none;">
<input type="text" class="form-control" name="institute" placeholder="Institute Name" value="<?php echo set_value('institute');?>">
<span class="text-danger"><?php echo form_error('institute'); ?></span>
</div>
<div class="form-group col-lg-10" id="professional" style="display: none;">
<input type="text" class="form-control" name="company" placeholder="Company Name" value="<?php echo set_value('company');?>">
<span class="text-danger"><?php echo form_error('company'); ?></span>
</div>
但验证在控制器中
$this->form_validation->set_rules('institute','Institute Name','trim|required');
$this->form_validation->set_rules('company','Company Name','trim|required');
如果不需要,请删除验证。 :)