5
Contributors
24
Replies
86
Views
3 Years
Discussion Span
Last Post by davy_yg
0

IF you are emailing the contents of a form. In your form handler:-

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com'); 
$this->email->cc('another@another-example.com'); 
$this->email->bcc('them@their-example.com'); 

$this->email->subject($yourFormSubject);
$this->email->message($concatOfFormFieldsYouWantToSend);    

$this->email->send();
0

Controller. A view is just for displaying a web page, or partials.

Edited by paulkd: more info

0

controller/ccontactus.php

<?php

include 'page.php';

class ccontactus extends Page {

    public function __construct(){

        parent::__construct();

        $this->data['assetscont'] = array(
            'contactcss' => base_url().'assets/css/contact.css'         
        );

    }

    public function index(){

        $this->load->view('templates/navigation', $this->data['navigation']);           
        $this->load->view('contactus',$this->data['assetscont']);
        $this->load->view('templates/footer', $this->data['footer']);
    }

    public function contact(){

            $this->load->library('email');
            $this->email->from('your@example.com', 'Your Name');
            $this->email->to('someone@example.com');
            $this->email->cc('another@another-example.com');
            $this->email->bcc('them@their-example.com');
            $this->email->subject($yourFormSubject);
            $this->email->message($concatOfFormFieldsYouWantToSend);
            $this->email->send();
    }   
}

?>

views/contactus.php

<div id="carticle3">

<div id="inputID">
<div id="title">Your Message: </div><br><br>
<input type="text" name="name" placeholder="Your name" value=""><br>
<input type="text" name="email" placeholder="Your email" value=""><br>
<input type="text" name="subject" placeholder="subject" value=""><br>
<textarea rows="4" cols="20" placeholder="Message"></textarea><br><br>
<input type="submit" name="submit" value="kirim" class="button">
</div>

</div>

Hello,

Can anyone help me makes this code works?

0

what do you mean? how to create the form in controllers/contactus.php?

Edited by davy_yg

0

What if I add this: <form name="contact" action="contactaction.php" method="POST"> under Your Message: ?

views/contactus.php

    <div id="carticle3">
    <div id="inputID">
    <div id="title">Your Message: </div><br><br>
    <form name="contact" action="contactaction.php" method="POST">
    <input type="text" name="name" placeholder="Your name" value=""><br>
    <input type="text" name="email" placeholder="Your email" value=""><br>
    <input type="text" name="subject" placeholder="subject" value=""><br>
    <textarea rows="4" cols="20" placeholder="Message"></textarea><br><br>
    <input type="submit" name="submit" value="kirim" class="button">
    </div>
    </div>

Then, what else should I do?

0

If I'm guessing correctly, this function: public function contact() has to accept paramaters.. Then you could do something like this:

<?php

  if(!isset($_POST['form_submitted']))
  {
       // output the form
  }else{

    $c = new ccontctus(); // constructor 
    $c->contact($_POST['uname'], blah, blah, blah);

Something like this?

0

A quick question davyg. What is the web address to your contact form.

e.g. www.mywebsite.com/contactus ?

0

davy_yg,

Your HTML document is lacking structure. I think you should spend time looking at HTML primers. You page doesn't have a single paragraph tag.

Your webserver doesn't appear to be set up correctly. Your PHP code is not being interpreted.

0

Yet, I still do not know in which file do I have to write the codes and how to?

This is how your controller should look like:

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name'); 
$this->email->to('someone@example.com'); 
$this->email->cc('another@another-example.com'); 
$this->email->bcc('them@their-example.com'); 

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

if (!$this->email->send())
{
    $this->load->view('contact_view');
} else
{
    $this->load->view('mail_successfully_sent_view');
}

Also, I would warmly suggest you see these lessons and read user guide again.
I learnt from few good video series from YT and it helps much.
There is much more things should be corrected in your code. I suppose you don't have set suffix in config neither. However, pay attention on difference between CI versions 1.7.2 and 2.x.x. to make all tasks from video clips work.

0

This flow might help
---controller
1. controller :(loads the form)

--- view
2. form : you can start the form by this
"echo form_open('[redirect to contact function for sending email]')"
perhaps contact funtion accepts parameters like "email" for from,"subject" and "body" on submit

note be sure you loaded the form helper to initiate form_open function else you can do it manually like

"<form method='POST' action='yourhost/controller/function'>"

---controller
3. then you can follow what "Tpojka" as the content or your contact function.

Edited by code739

0

This is what I have for the controller now:

controllers/ccontactus.php

class ccontactus extends Page {

public function contact(){

        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'name', 'required');
        $this->form_validation->set_rules('email', 'email', 'required');
        $this->form_validation->set_rules('subject', 'subject', 'required');
        $this->form_validation->set_rules('message', 'message', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('contactus');
        }
        else
        {
            $this->load->view('contactus');
        }


        $this->load->helper(array('form', 'url'));

        $this->load->library('email');
        $this->email->from('your@example.com', 'Your Name');
        $this->email->to('someone@example.com');
        $this->email->cc('another@another-example.com');
        $this->email->bcc('them@their-example.com');
        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');
        if (!$this->email->send())
            {
            $this->load->view('contactus');
            } else
            {
            $this->load->view('contactus_emailsent');
            }   
    }

}

views/contactus.php

<?php echo form_open('contact', array('id'=>'contactform')); ?>

<div id="title">Your Message: </div><br><br>

<form name="contact" action="contactaction.php" method="POST">
<input type="text" name="name" placeholder="Your name" value=""><br>
<input type="text" name="email" placeholder="Your email" value=""><br>
<input type="text" name="subject" placeholder="subject" value=""><br>
<textarea rows="4" cols="20" placeholder="Message"></textarea><br><br>
<input type="submit" name="submit" value="kirim" class="button">
</div>

</div>

How to make the form works by sending email to the intended receipient when I press the submit button?

This form does not works yet.

Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\IndonusaCI\application\views\contactus.php on line 70

line 70: <?php echo form_open('contact', array('id'=>'contactform')); ?>

Edited by davy_yg

0

ok, I did change somethings. The same error still appears:

Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\IndonusaCI\application\views\contactus.php on line 70

Is my line 70 code correct?

line 70: <?php echo form_open('contact', array('id'=>'contactform')); ?>

0

ok, I did autoload helper:

autoload.php

$autoload['helper'] = array('url', 'form');

I still have error:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: helpers/form_helper.php

Line Number: 264

0

That error you should solve with adding name parameter to your textarea [em]dataMessage[/em] array.
It would be good to add name parameter to every input field.

0

Hello,

The following codes produce correct views. It just don't function correctly. If I press the kirim / submit button assuming that I do not fill in the required fields, no error appears.

What should I do so that the form function correctly, by showing errors whenever I do not fill in the required field and also sent the messages to the intended address if all the required field have been filled and the kirim / submit button has been pressed.

ccontrollers/contactus.php

<?php

include 'page.php';

class Ccontactus extends Page {

    public function __construct(){

        parent::__construct();

        $this->data['assetscont'] = array(
            'contactcss' => base_url().'assets/css/contact.css'         
        );

    }

    public function index(){

        $this->load->view('templates/navigation', $this->data['navigation']);           
        $this->load->view('contactus',$this->data['assetscont']);
        $this->load->view('templates/footer', $this->data['footer']);
    }

    public function contact(){

             $this->load->library('form_validation');
             $this->form_validation->set_rules('name', 'name', 'required');
             $this->form_validation->set_rules('email', 'email', 'required');
             $this->form_validation->set_rules('subject', 'subject', 'required');
             $this->form_validation->set_rules('message', 'message', 'required');

             if ($this->form_validation->run() == FALSE)
                {
                $this->load->view('contactus');
                }
             else
                {



             $this->load->library('email');
             $this->email->from('davy_yg@yahoo.com', 'Your Name');
             $this->email->to('davy_yg@yahoo.com');
             $this->email->cc('another@another-example.com');
             $this->email->bcc('them@their-example.com');
             $this->email->subject('Email Test');
             $this->email->message('Testing the email class.');

             if (!$this->email->send())
                {
                $this->load->view('contactus');
                } else
                {
                $this->load->view('contactus_emailsent');
                } 

                }
             }  

}

?>

views/contactus.php

<div id="title">Your Message: </div><br><br>

<?php echo form_open('ccontactus/contact', array('id'=>'contactform')); 

      $dataName = array(
              'name'        => 'name',
              'id'          => 'name',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'style'       => 'width:32%',
              'placeholder' => 'Your name'
            );

      echo form_input($dataName); ?><br>
<?php      

      $dataEmail = array(
              'name'        => 'email',
              'id'          => 'email',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'style'       => 'width:32%',
              'placeholder' => 'Your email'
            );

      echo form_input($dataEmail); ?><br>
<?php      

      $dataSubject = array(
              'name'        => 'subject',
              'id'          => 'subject',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'placeholder' => 'Your subject'
            );

      echo form_input($dataSubject);

?><br>
<?php      

      $dataMessage = array(
              'name'        => 'message',
              'id'          => 'message',
              'value'       => '',
              'cols'        => '40',
              'rows'        => '4',
              'style'       => 'width:37%',
              'placeholder' => 'message'
            );

      echo form_textarea($dataMessage);

?><br>
<?php      

      echo form_submit(array('class' => 'button', 'name' => 'kirim'), 'kirim');

?><br>

</div>

<?php form_submit() ?>

</div>
This topic has been dead for over six months. 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.