Codeigniter 中使用表单的电子邮件类

发布于 2024-12-17 12:30:00 字数 2864 浏览 0 评论 0原文

免责声明:我是网络开发新手。

场景:我创建了一个联系表单,并尝试将输入传递到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-12-24 12:30:00

好吧,你在这里几乎没有定义任何变量:

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');
    }

$email$subject$message 来自哪里?
您可能想要使用类似的东西(但我建议您做得更好:))

$email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');

此外,请确保您在调用电子邮件库之前已加载它,并且在 else{} 中加载视图部分(因为你省略了它我无法告诉)

Well, you barely define any variable here:

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');
    }

Where do $email, $subject, $message come from?
You might want to use something like (but I suggest you do better :))

$email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文