0

Hello.
I'am using php codeigniter for my task here. i just have to use it.
its all set up at the first. so i kinda not understand the path.
I've read some of the explainatiion and try from scratch
but when I try to apply it to my task its not working and even make me more confusing.
So anyone here can help me to solve these submit form to email issue?
This is the final thing beforei I can submit my task.
I got no problem to use codeigniter to route my files and all
I just stuct at this submit form to email. for 2 week! seem so stupid
really hope someone can guide me here.
thank you!

2
Contributors
13
Replies
55
Views
3 Months
Discussion Span
Last Post by KagaKuro
Featured Replies
  • 2
    cereal 1,354   3 Months Ago

    Hello KK, from the screenshot is seems you are trying to load CI resources from outside the application folder. Is Contact.php a CI controller? Can you share it? Remember to remove address and password as the post is public. Read More

  • 1
    cereal 1,354   3 Months Ago

    Okay, consider to use the identical operator `===` on line 20: if ($this->form_validation->run() == FALSE) The `alpha_space_only()` callback can fail when using accented characters like `àèéìòùñ`, so you may want to replace the regex pattern to: preg_match('/^[\p{L} ]+$/ui', $str) a part this your code seems fine. > But another error … Read More

  • 1
    cereal 1,354   3 Months Ago

    It, probably, happens because you are calling the session inside `application/core/MY_Loader.php` but you are loading it from the controller, which is executed after the MY_Loader. Have you tried to autoload the session? Read More

  • 1
    cereal 1,354   3 Months Ago

    Open *application/config/autoload.php* and set **session** inside: $autoload['libraries'] = array('session'); You can do the same with other libraries or helpers that you will use constantly, like database or form. More info: https://codeigniter.com/user_guide/general/autoloader.html Read More

  • 1
    cereal 1,354   3 Months Ago

    Hehe, sure you can! If you want to solve it, instead, read the notice, it says *Use of undefined constant session - assumed 'session'*, which means you probably wrote: $autoload['libraries'] = array(session); # without quotes Instead of: $autoload['libraries'] = array('session'); # with quotes By adding quotes the value is considered … Read More

0

Capture.PNG

my problem;

  1. the data will not validate (sent even there is no input )
  2. and the php error in the box,
  3. and ofcourse, it is not been send to email.

Edited by KagaKuro

2

Hello KK,

from the screenshot is seems you are trying to load CI resources from outside the application folder. Is Contact.php a CI controller? Can you share it? Remember to remove address and password as the post is public.

0

yes. I did put the contact.php in the root file.

but I've change it and put it in the controller

<?php
class Contactform extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library(array('session', 'form_validation', 'email'));
    }

    function index()
    {
        //set validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
        $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
        $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');

        //run validation on form input
        if ($this->form_validation->run() == FALSE)
        {
            //validation fails
            $this->load->view('contact_form_view');
        }
        else
        {
            //get the form data
            $name = $this->input->post('name');
            $from_email = $this->input->post('email');
            $subject = $this->input->post('subject');
            $message = $this->input->post('message');

            //set to_email id to which you want to receive mails
            $to_email = 'user@gmail.com';

            //configure email settings
            $config['protocol'] = 'smtp';
            $config['smtp_host'] = 'ssl://smtp.gmail.com';
            $config['smtp_port'] = '465';
            $config['smtp_user'] = 'mail@mail.my';
            $config['smtp_pass'] = 'password';
            $config['mailtype'] = 'html';
            $config['charset'] = 'iso-8859-1';
            $config['wordwrap'] = TRUE;
            $config['newline'] = "\r\n"; //use double quotes
            //$this->load->library('email', $config);
            $this->email->initialize($config);                        

            //send mail
            $this->email->from($from_email, $name);
            $this->email->to($to_email);
            $this->email->subject($subject);
            $this->email->message($message);
            if ($this->email->send())
            {
                // mail sent
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
                redirect('contactform/index');
            }
            else
            {
                //error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
                redirect('contactform/index');
            }
        }
    }

    //custom validation function to accept only alphabets and space input
    function alpha_space_only($str)
    {
        if (!preg_match("/^[a-zA-Z ]+$/",$str))
        {
            $this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
?>

But another error happen. did i have to setup anything before I can make the code works?

1

Okay,

consider to use the identical operator === on line 20:

if ($this->form_validation->run() == FALSE)

The alpha_space_only() callback can fail when using accented characters like àèéìòùñ, so you may want to replace the regex pattern to:

preg_match('/^[\p{L} ]+$/ui', $str)

a part this your code seems fine.

But another error happen.

So, what is the error?

1

It, probably, happens because you are calling the session inside application/core/MY_Loader.php but you are loading it from the controller, which is executed after the MY_Loader. Have you tried to autoload the session?

0

I've add it and it give me error at my fast-store-list page ( a page that I used iFrame )

session.PNG

This error appear above my webpage.

Can I just give up . haaaaaaaaaaaa

1

Hehe, sure you can!

If you want to solve it, instead, read the notice, it says Use of undefined constant session - assumed 'session', which means you probably wrote:

$autoload['libraries'] = array(session); # without quotes

Instead of:

$autoload['libraries'] = array('session'); # with quotes

By adding quotes the value is considered a string, which is what you need in this case.

0

hey cereal. can you help me set up the controller and stuff ? or you cant...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.