<?php
Class Register extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->helper(array("form","url"));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('email');
$this->load->view("register_view");
$this->load->model("Register_model");
$this->load->database();
}
public function index(){
$this->form_validation->set_rules('uname', 'Name', 'required|min_length["3"]',array("required"=>"username is required","min_length"=>"min of 3char"));
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('mobile','Mobile','required');
$this->form_validation->set_rules('pwd','Password','required|min_length["6"]');
$this->form_validation->set_rules('cpwd','Confirm Password','required|matches["pwd"]');
$this->form_validation->set_rules('terms','Terms & Conditions','required');
if($this->form_validation->run()==true){
$username=$this->input->post("uname");
$email=$this->input->post("email");
$password=$this->input->post("pwd");
$mobile=$this->input->post("mobile");
$state=$this->input->post("state");
$gender=$this->input->post("gender");
$terms=$this->input->post("terms");
// $uid=$this->input->post("");
$status=$this->register_model->insertData($name,$email,$password,$mobile,$state,$gender,$terms);
if($status==true){
$config=array('protocol'=>'smtp',
'smtp_host'=>'ssl://smtp.gmail.com',
'smtp_timeout'=>10,
'smtp_port'=>465,
'smtp_user'=>'r********@gmail.com',
'smtp_pass'=>'********',
'charset'=>'utf-8',
'mailtype'=>'html',
'newbie'=>'rn',
);
$to=$email;
$subject="Acc activation -$uname";
$message="Hi. your acc created. pkease clicl here to activate<br/>
<a href='base_url()'register/activate/uniid >Activate</a> ";
$this->email->intialize($config);
$this->email->set_newline("rn");
$this->email->set_crif("rn");
$this->email->from('r**********@gmail.com','company');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send()){
$this->session->set_tempdata("success","account created please activate",2);
redirect(current_url());
}
else{
print_r($this->email->print_debugger());
}
}
else{
print_r($this->email->print_debugger());
}
}
}
}
?>
<html>
<head>
</head>
<body>
<h1>Register form</h1>
<?php if($this->session->tempdata("error")){
echo"<p>". $this->session->tempdata("error"). "</p>";
}
if($this->session->tempdata("success")){
echo"<p>".$this->session->tempdata("success"). "</p>";
}
?>
<table>
<tr>
<td>Name </td>
<td><input type="text" name="uname" value="<?php echo set_value('uname'); ?>" /></td>
</tr>
<tr>
<td>Email </td>
<td><input type="text" name="email" value="<?php echo set_value('email'); ?>" /></td>
</tr>
<tr>
<td>Password </td>
<td><input type="password" name="pwd" value="" /></td>
</tr>
<tr>
<td>Confirm Password </td>
<td><input type="password" name="cpwd" value="" /></td>
</tr>
<tr>
<td>Mobile </td>
<td><input type="text" name="mobile" value="<?php echo set_value('mobile'); ?>" /></td>
</tr>
<tr>
<td>Gender </td>
<td>
<label>
<input type="radio" name="gender" value="male" <?php if(set_value('gender')=='male') echo 'checked'; ?> /> Male
</label>
<label>
<input type="radio" name="gender" value="female" <?php if(set_value('gender')=='female') echo 'checked'; ?> /> Female
</label>
</td>
</tr>
<tr>
<td>Select state </td>
<td>
<select name="state">
<option value="">---Select State----</option>
<option <?php if(set_value("state")=="Delhi") echo 'selected'; ?> value="delhi" >Delhi</option>
</select>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" name="terms" value="1" <?php if(set_value("terms")=="1") echo "checked"; ?> />
Please accept terms and condiotions
</label>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Register" /></td>
</tr>
在这段代码中,我将控制器保存为Register.php,将视图保存为Register_view.php,加载了表单验证、会话、电子邮件和帮助程序库,如表单、url,这些都是必需的。我还在autoload.php文件中加载了这些。但它并没有显示表单验证错误、设置值。它只是提交并显示同一页面。需要做哪些改变。提前谢谢。
您需要执行类似的操作
if($this->form_validation->run() === FALSE) {
$this->load->view("add");
return false;
}
请添加if($this->form_validation->run()==true){}else{}
条件的负载视图末尾。
if($this->form_validation->run()==true){
}
$this->load->view("register_view");
请试试这个。
请尝试以下代码:
if ($this->form_validation->run()) {
//Do your code
}else{
echo validation_errors();
die;
}
$this->load->view('layout/admin');