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!

Recommended Answers

All 13 Replies

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.

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.

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?

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?

0418.PNG

This post has no text-based content.

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?

no. i did not try. how can i do it?

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

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

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.

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

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

Hi, what do you mean?

its okay. things just got complicated. dumb me haha

commented: no problem, bye :) +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.