我是PHP的新手,并尝试创建一个简单的页面,用户可以在其中请求他们想要的某些服务。通过验证后,我将表单数据保存到数据库中。当有人要求提供某些服务时,该程序还应发送电子邮件通知。所以,在此之前我的代码正在运行。
但是,当表单成功提交时,我希望在电子邮件正文/消息中获取表单数据,但到目前为止我无法完成。我用Google搜索并尝试了一些但是没有用。
如何使用codeigniter进行操作?有人可以指导我如何解决这个问题?如果您需要完整的HTML代码,请告诉我们。我很感激你的帮助。
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();
}
}
}
?>
表示复选框部分代码
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(型号)
<?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);
}
}
?>
答案 0 :(得分:1)
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:根据需要进行设计
<html>
<div>
<label>Name</label>
<?php echo $f_name." ".$l_name; ?>
</div>
<div>
<label>Comment</label>
<?php echo $comment; ?>
</div>
</html>