Codeigniter 中使用表单的电子邮件类
免责声明:我是网络开发新手。
场景:我创建了一个联系表单,并尝试将输入传递到 CodeIgniter 中使用的电子邮件类函数中。提交表单时,我收到未定义的变量错误。电子邮件库会自动加载以供参考。已经很晚了,我可能会忽略一些简单的事情,但发帖也没什么坏处。非常感谢您的帮助!
控制器:
public function index()
{
//form validation
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['subject'] = array(
'name' => 'subject',
'id' => 'subject',
'type' => 'text',
'value' => $this->form_validation->set_value('subject'),
);
$this->data['message'] = array(
'name' => 'message',
'id' => 'message',
'type' => 'text',
'value' => $this->form_validation->set_value('message'),
);
if ($this->form_validation->run() == true)
{
$this->email->from($email);
$this->email->to('[email protected]');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
redirect('contact/success');
}
else
{
$this->data['error_message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error_message');
$title['title'] = 'Contact';
$this->load->view('public/head_view', $title);
$this->load->view('public/header_view');
$this->load->view('public/contact_view', $this->data);
$this->load->view('public/footer_view');
}
视图:
<div id="infoMessage"><?php echo $error_message;?></div>
<?php $attritubes = array('class' => 'nice'); ?>
<?php echo form_open('contact'); ?>
<p>Email Address:<br />
<?php echo form_input($email); ?>
</p>
<p>Subject:<br />
<?php echo form_input($subject); ?>
</p>
<p>Message:<br />
<?php echo form_textarea($message); ?>
</p>
<p><?php echo form_submit('submit', 'Submit'); ?></p>
<?php echo form_close(); ?>
Disclaimer: I'm new to web develoment.
Scenario: I've created a contact form, and I'm trying to pass the input into the email class functions used in CodeIgniter. I am receiving undefined variable errors when the form is submitted. Email library is autoloaded for reference. It's late, and I may be overlooking something easy, but it doesn't hurt to post. Thanks a lot for all of your help!
Controller:
public function index()
{
//form validation
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['subject'] = array(
'name' => 'subject',
'id' => 'subject',
'type' => 'text',
'value' => $this->form_validation->set_value('subject'),
);
$this->data['message'] = array(
'name' => 'message',
'id' => 'message',
'type' => 'text',
'value' => $this->form_validation->set_value('message'),
);
if ($this->form_validation->run() == true)
{
$this->email->from($email);
$this->email->to('[email protected]');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
redirect('contact/success');
}
else
{
$this->data['error_message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error_message');
$title['title'] = 'Contact';
$this->load->view('public/head_view', $title);
$this->load->view('public/header_view');
$this->load->view('public/contact_view', $this->data);
$this->load->view('public/footer_view');
}
View:
<div id="infoMessage"><?php echo $error_message;?></div>
<?php $attritubes = array('class' => 'nice'); ?>
<?php echo form_open('contact'); ?>
<p>Email Address:<br />
<?php echo form_input($email); ?>
</p>
<p>Subject:<br />
<?php echo form_input($subject); ?>
</p>
<p>Message:<br />
<?php echo form_textarea($message); ?>
</p>
<p><?php echo form_submit('submit', 'Submit'); ?></p>
<?php echo form_close(); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你在这里几乎没有定义任何变量:
$email
、$subject
、$message
来自哪里?您可能想要使用类似的东西(但我建议您做得更好:))
此外,请确保您在调用电子邮件库之前已加载它,并且在
else{}
中加载视图部分(因为你省略了它我无法告诉)Well, you barely define any variable here:
Where do
$email
,$subject
,$message
come from?You might want to use something like (but I suggest you do better :))
Also, make sure you've loaded the email library before calling it, and that you load the view in your
else{}
part (since you omitted it I cannot tell)