[英]Sending form data to an email after a successful submission using codeigniter
I am trying to make a simple page where users will request for some services they would like to have.我正在尝试制作一个简单的页面,用户将在其中请求他们想要的某些服务。 After passing the validation I save the form data into a database.
通过验证后,我将表单数据保存到数据库中。 This program should also send an email notification when someone ask for some services.
当有人要求某些服务时,该程序还应发送电子邮件通知。 So, Until this my code is working.
所以,在此之前我的代码正在运行。
But I want to get the form data in the email body/message when a form is successfully submited but so far I could not manage to do it.但是我想在表单成功提交时获取电子邮件正文/消息中的表单数据,但到目前为止我无法做到。 I googled and tried some but did not work.
我用谷歌搜索并尝试了一些,但没有奏效。
How can I do it using codeigniter?我如何使用 codeigniter 做到这一点? Could someone guide me how can I fix this?
有人可以指导我如何解决这个问题吗? If you need full HTML code please let me know.
如果您需要完整的 HTML 代码,请告诉我。 I would appreciate your help.
我会很感激你的帮助。
Contact.php file (Controller) Contact.php 文件(控制器)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller {
function __construct()
{
parent::__construct();
//load the contact_model
$this->load->model('contact_model');
}
public function index()
{
//load view pages
$this->load->view('Template/header');
$this->load->view('contact');
$this->load->view('Template/footer');
}
public function validate_form()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//FORM VALIDATION
//First Name
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|min_length[3]|max_length[17]');
//Last Name
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|min_length[3]|max_length[17]');
//User Name
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|alpha|strtolower|min_length[3]');
//Email
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
//Manager's Full Name
$this->form_validation->set_rules('m_name', 'Full Name', 'trim|required');
//Comment
$this->form_validation->set_rules('comment', 'Comment', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
//echo validation_errors();
$this->load->view('Template/header');
$this->load->view('contact');
$this->load->view('Template/footer');
//$redirect = $this->input->post('url');
//$this->session->set_flashdata('errors',validation_errors());
}
else
{
$specialised_category = $this->input->post('checkbox_cat');
$data = array(
'f_name' => $this->input->post('first_name'),
'l_name' => $this->input->post('last_name'),
'user_name' => $this->input->post('user_name'),
'email' => $this->input->post('email'),
'fullname' => $this->input->post('m_name'),
'comment' => $this->input->post('comment'),
'city' => $this->input->post('city'),
//encoding to JSON and comma sepated values
'services_list' => json_encode(implode(",", $specialised_category))
);
//inserting data
//$this->db->insert('sysops_tbl', $data);
$this->contact_model->insert_into_db($data);
//load the form success message once submitted correctly
$this->load->view('formsuccess');
$this->send($data);
//redirect to the main page
//redirect(base_url());
}
}
public function send($value='')
{
//load the email library
$this->load->library('email');
//Sending email
$config_email = array(
'mailtype' => 'html',
'charset' =>'iso-8859-1',
'wordwrap' => TRUE
);
//debug
//print_r($value);
//override the config from text to html
$this->email->initialize($config_email);
//printing manager email and name
$this->email->from($this->input->post('email'),$this->input->post('m_name'));
//receipant email
$this->email->to('xxx.xx@xxx.com');
//header
//$this->email->set_header(json_encode(implode(",", $this->input->post('checkbox_cat'))),'binded');
//email subject
$this->email->subject('We need user access for');
//want to inject values here in the email message
$this->email->message('Check above !!:');
//print the message if email is sent
if($this->email->send())
{
return TRUE;
//echo "Email is sent";
}else
{
return FALSE;
//echo $this->email->print_debugger();
}
}
}
?>
for check boxes partial code is given对于复选框,给出了部分代码
contact.php (views) contact.php(查看次数)
<div class="form-group">
<label class="col-md-4 control-label">Which ?</label>
<div class="col-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="checkbox_cat[]" class="get_value" value="option4" /> option4
</label>
<label>
<input type="checkbox" name="checkbox_cat[]" class="get_value" value="option5" /> option5
</label>
<label>
<input type="checkbox" name="checkbox_cat[]" class="get_value" value="option6" /> option6
</label>
<label>
<input type="checkbox" name="checkbox_cat[]" class="get_value" value="option7" /> option7
</label>
</div>
</div>
contact_model.php (model) contact_model.php(模型)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class contact_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function insert_into_db($data)
{
// Inserting into database table
$this->db->insert('sysops_tbl', $data);
}
}
?>
public function send($value='')
{
//load the email library
$this->load->library('email');
//Sending email
$config_email = array(
'mailtype' => 'html',
'charset' =>'iso-8859-1',
'wordwrap' => TRUE
);
$msg = $this->load->view('email_template',$value,true);
//override the config from text to html
$this->email->initialize($config_email);
//printing manager email and name
$this->email->from($this->input->post('email'),$this->input->post('m_name'));
//receipant email
$this->email->to('xxx.xx@xxx.com');
//header
//$this->email->set_header(json_encode(implode(",", $this->input->post('checkbox_cat'))),'binded');
//email subject
$this->email->subject('We need user access for');
$this->email->message($msg);
//print the message if email is sent
if($this->email->send())
{
return TRUE;
//echo "Email is sent";
}else
{
return FALSE;
//echo $this->email->print_debugger();
}
}
email_template : Design it as you required email_template :根据您的需要设计
<html>
<div>
<label>Name</label>
<?php echo $f_name." ".$l_name; ?>
</div>
<div>
<label>Comment</label>
<?php echo $comment; ?>
</div>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.