These are chat archives for codeigniter-id/codeigniter-id.github.io

27th
Apr 2017
Netlinqs
@netlinqs
Apr 27 2017 06:24
@DahlitzFlorian my demo is made with codeigniter. Only the views are in html.
Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 06:42
So you can import in your controller (which is called after form submit) the email library from CodeIgniter. To see a simple example just click on this link https://www.codeigniter.com/user_guide/libraries/email.html it directs you to the codeigniter documentation which is very good ๐Ÿ˜Š so try it yourself and if you are unsure and not able to master it on your own just send me a quick message.
Netlinqs
@netlinqs
Apr 27 2017 07:42

@DahlitzFlorian ok, i'have set a new controller called Email with this code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Email extends CI_Controller {

  $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.');

  $this->email->send();
}
?>

after a new views with this code:

<form class="uk-grid-medium" action="" method="post" uk-grid>

      <div class="uk-width-1-1 uk-width-1-2@l uk-width-1-2@xl">
        <input class="uk-input" type="text" id="name" name="name" pattern="[a-zA-Z\s]+" title="inserisci solo lettere." placeholder="Nome" required>
      </div>

      <div class="uk-width-1-1 uk-width-1-2@l uk-width-1-2@xl">
        <input class="uk-input" type="email" id="email" name="email" pattern="^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$" placeholder="E-mail" required>
      </div>

      <div class="uk-width-1-1">
        <textarea style="height:150px;" class="uk-textarea" id="message" name="message" placeholder="Messaggio" required></textarea>
      </div>

      <div class="uk-width-1-1 uk-width-1-2@s uk-width-1-2@m uk-width-1-2@l uk-width-1-2@xl">
        <input class="uk-checkbox" type="checkbox" id="checkbox" name="checkbox" value="si">
        <span>Accetto la</span>
        <a class="link-privacy" href="#privacy" uk-toggle>Privacy Policy</a>
        <!-- PRIVACY qui c'รจ l'include della privacy -->

      </div>

      <div class="uk-width-1-1 uk-text-left@m uk-text-left@m uk-text-center">
        <button class="uk-button uk-button-default uk-button-large" type="submit" id="button" name="submit" value="submit">INVIA MESSAGGIO</button>
      </div>

    </form>

And finally i have set the routes:

$route['email'] = 'pages/Email';

Is correct?

Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 07:47
You don'T need to set $route to a certain value in this case. In your view you have to change the value of the action"" attribute of your form, so that it will go to your Email-Controller. There you can fetch the post data via the command
$this->input->post('field_name') and put it into the methods of the email library (subject and so on).
Netlinqs
@netlinqs
Apr 27 2017 07:50
ok, i remove the $route because is not necessary.
Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 07:50
But be aware that this is a very simple way to do it. If I have some time today I will make a good example for you and send you a link to the gist, ok?
Netlinqs
@netlinqs
Apr 27 2017 07:51
oh @DahlitzFlorian i would be very grateful!
After i have set the action:
<form class="uk-grid-medium" action="<?php echo base_url('index.php/application/controllers/Email.php'); ?>" method="post" uk-grid>
but my action is not correct. I have an error 403.
Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 08:01
I've always removed index.php with the use of a .htaccess file. So either this should work for you:
<form class="uk-grid-medium" action="<?php echo base_url('index.php/email'); ?>" method="post" uk-grid>
or this:
<form class="uk-grid-medium" action="<?php echo base_url('email'); ?>" method="post" uk-grid>
Netlinqs
@netlinqs
Apr 27 2017 08:03
i have error 404
Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 08:05
because codeigniter is doing the following:
It has a look at the url and separate it into segments. the first is of course the base url such as www.codeigniter.com/ or localhost/
the next that follows is either a controller name or a folder name in which to look for the following controller name. in this case it would be:
localhost/email
to search for the controller (codeigniter looks always in application/controller thats why you don't need to write it explicitly). after that he uses the index method if nothing is after the controller name or the method name that comes after like:
localhost/email/send
after that can stand further get-parameter for the method
please send me the url which is called after submitting the code
Netlinqs
@netlinqs
Apr 27 2017 08:08
Yes, i have set .htaccess for "short url" and remove index.php. the url show is: http://samuelesantoni.com/demo/test/index.php/email
Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 08:10
ah yeah that's what i wrote above: it tries to call the index method of the email controller - which does not exists
so the code you wrote in your email class/controller has to be in an index method which is in your email controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Email extends CI_Controller {
  public function index() {
    $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.');

    $this->email->send();
  }
}
?>
Netlinqs
@netlinqs
Apr 27 2017 08:16

So, the code in my controller called Email:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Email extends CI_Controller {

  public function index() {
    $this->load->library('email');

    $this->email->from('your@example.com', 'Your Name');
    $this->email->to('info@XXXXXXXXXX.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.');

    $this->email->send();
  }
}
?>

and the code inside action tag:

<form class="uk-grid-medium" action="<?php echo base_url('email'); ?>" method="post" uk-grid>
the error is always 404
Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 08:18
you are also not calling any view after all
So as i said: I will create a detailed example for you if I have the time today. CU
Netlinqs
@netlinqs
Apr 27 2017 08:19
ok, @DahlitzFlorian thanks!
Florian Dahlitz
@DahlitzFlorian
Apr 27 2017 08:19
you're welcome
Netlinqs
@netlinqs
Apr 27 2017 08:19
:)