这是我的简单注册。我想在提交此注册form.How按钮后发送电子邮件给用户,我可以这样做吗??提前谢谢。1.在视图中,我做了一个简单的注册表,如您所见,我添加了一些基本信息。2.在我的控制器中添加了验证。并将它们保存在数据库中。4.模型具有简单的插入查询。
form_view.php
<table>
<tr>
<td>Name</td><td><?php echo form_input($name);?></td>
<td><?php echo form_error('name');?></td>
</tr>
<tr>
<td>Email</td><td><?php echo form_input($email);?></td>
<td><?php echo form_error('email');?></td>
</tr>
<tr>
<td>Phone</td><td><?php echo form_input($phone);?></td>
<td><?php echo form_error('phone');?></td>
</tr>
<tr>
<td>Address</td><td><?php echo form_input($address);?></td>
<td><?php echo form_error('address');?></td>
</tr>
<tr>
<td>Division</td><td><?php echo form_dropdown('division',$division,'Please Select');?></td>
</tr>
<td></td><td><?php echo form_submit($submit);?></td>
</tr>
</table>main.php
<?php
class main extends CI_Controller{
public function viewForm(){
$this->load->view('form_view');
}
public function insertData(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Full Name', 'required|min_length[5]|max_length[12]|is_unique[address.name]');
$this->form_validation->set_rules('phone', 'Contact field', 'required|min_length[5]|max_length[12]|is_unique[address.phone]');
$this->form_validation->set_rules('address', 'Full Address', 'required|min_length[5]|max_length[12]|is_unique[address.address]');
$this->form_validation->set_rules('email', 'Email Address', 'required|min_length[5]|max_length[12]|is_unique[address.email]');
if($this->form_validation->run()==FALSE){
$this->load->view('form_view');
}else{
$sql="Select MAX(id) from address";// Auto Increment
$query= mysql_query($sql);
$selectid= mysql_fetch_array($query);
$finalid=$selectid[0]+1;
$data=array();
$data['id']=$finalid;
$data=array();
$data['name']=$this->input->post('std_name');
$data['phone']=$this->input->post('std_phone');
$data['address']=$this->input->post('std_address');
$data['division']=$this->input->post('division');
$data['email']=$this->input->post('email');
$this->load->model('main_model');
$this->main_model->save_user($data);
redirect('main/viewForm');
}
}main_model.php
<?php
class main_model extends CI_Model{
public function save_user($data){
$this->db->insert('address',$data);
}
?>
}
?>发布于 2015-02-03 20:49:13
您可能需要考虑在用户info表中添加两列。比如varchar activation_code & tinyint activated。在保存用户信息时,生成激活码,并在您的应用程序url的链接中向用户发送带有该代码的邮件,并使用激活的false值为该特定用户保存该代码。验证使用该链接的代码是否已激活为true。这将是我的方法。
发布于 2015-02-03 21:16:15
在我们开始之前,我只想指出你的控制器中不应该有查询。该零件必须在模型中。
要发送电子邮件,您可以使用codeigniter的库:https://ellislab.com/codeigniter/user-guide/libraries/email.html
以下是如何在您的案例中使用它:
if($this->form_validation->run()==FALSE)
{
$this->load->view('form_view');
}
else
{
$email = $this->input->post('email');
//Your stuff before the redirect
//If you have any particular smtp configuration (here, gmail)
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_user' => 'myemail@gmail.com',
'smtp_pass' => 'mypassword',
'smtp_port' => '465',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('MyName@MySite.com', 'MyName');
$this->email->to($email);
$this->email->subject('Mail subject');
$msg = "Welcome to my website !";
$this->email->message($msg);
if($this->email->send())
{
//Stuff if mail succeded
}
else
{
//Stuff if mail failed
}
redirect('main/viewForm');
}https://stackoverflow.com/questions/28299162
复制相似问题