[eluser]behnampmdg3[/eluser]
Hello friends;
I am new to CI and MVC. This is a very basic email validation (both format and with database) system.
I was wondering if I am doing ok or I am totally off track. Please comment or give me tips that I could improve my knowledge and turn them to skills and I become invincible. Thanks
Model
Code:
class Login_model extends CI_Model
{
function valid_member($email)
{
$query = "SELECT * FROM members WHERE email='".$email."'";
$database_results= $this->db->query($query);
if($database_results->num_rows()==1)
{
foreach ($database_results->result() as $row)
{
$data['this_name'] = $row->name;
}
$this->load->vars($data);
return true;
}
else
{
return false;
}
}
}
View
Code:
<div id="common_div"><?php echo $validation_results; echo $this_name;?>
<form action ="<?php echo site_url();?>register" method="post">
<table>
<tr>
<td>Email</td>
<td><input class = "text-box" type="text" name="email" value="<?php echo $email;?>" id="email" /></td>
</tr>
<tr>
<td>Password</td>
<td><input class = "text-box" type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register" name="submit" class="input_submit"/></td>
</tr>
</table>
</form>
</div>
Controller
Code:
class Register extends CI_Controller
{
public function index()
{
$data['title'] = "Register Page";
$this->load->vars($data);
$this->form();
}
public function form()
{
if(isset($_POST['submit']))
{
if($this->validate($this->input->post('email')))
{
}
}
$this->view_things();
}
public function validate($email=NULL)
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_validate_membership');
if ($this->form_validation->run())
{
return true;
}
else
{
return false;
}
}
public function validate_membership($email)
{
$this->load->model('login_model');
if($this->login_model->valid_member($email))
{
return true;
}
else
{
$this->form_validation->set_message('validate_membership', 'This email is not registered in the database');
return false;
}
}
public function view_things()
{
$this->load->view('header_view');
$this->load->view('register_view');
$this->load->view('footer_view');
}
}