-1

Hi guys i'm creating a new project with codeigniter and bootstrap.

I've configurated the first page and CI is loading everything, but i got a problem with the contact form. If i try to use it without codeigniter is working good so i think the problem is in the controller.

that's my code: (that's the view) landing.php

<div class="col-md-8 col-md-offset-2">
    <form action="%3C?php%20echo%20site_url('landing/index');%20?%3E" class=
    "wow bounceInUp" data-wow-delay="0.2s" data-wow-offset="10" id=
    "contact-form" method="post" name="contact-form" onsubmit=
    "return checkform();">
      <input name="act" type="hidden" value="act">

      <div class="row">
        <div class="col-md-6" style="margin-bottom:5px">
          <input class="form-control input-lg" id="name" name="name"
          placeholder="Name" required="required" type="text" value="name">
        </div>

        <div class="col-md-6" style="margin-bottom:5px">
          <input class="form-control input-lg" id="email" name="email"
          placeholder="E-mail" required="required" type="email" value="email">
        </div>
      </div>

      <div class="row">
        <div class="col-md-12" style="margin-bottom:5px">
          <div class="form-group">
            <input class="form-control input-lg" id="object" name="object"
            placeholder="Object" required="required" type="text" value=
            "object">
          </div>

          <div class="form-group" id="interested">
            <select class="form-control input-lg" id="contact-form" name=
            "interested">
              <option value="select">
                --Select--
              </option>

              <option id="info" value="info">
                Info
              </option>

              <option id="careers" value="careers">
                Careers
              </option>
            </select>
          </div>

          <div class="form-group" style="margin-bottom:5px">
            <textarea class="form-control" name="message" rows="3">
</textarea>
          </div><button class="btn btn-primary" style="width:100%;" type=
          "submit">Send</button>
        </div>
      </div>
    </form>
  </div>

and this is the controller landing.php;

class Landing extends CI_Controller {
public function index(){
    $this->load->view('header/landing.php');
    $this->load->view('landing.php');
    $this->load->view('footer/landing.php');

    if(isset($_POST['act']) && $_POST['act'] =="act"){
          $name = $_POST['name'];
          $object = $_POST['object'];
          $email   = $_POST['email'];
          $message = $_POST['message'];
          $interested = $_POST['interested'];
          if ($interested == 'info') { 
            $to = 'info@mymail.it';
            }
                else if ($interested == 'careers') { 
                $to = 'careers@mymail.it'; 
                }
          $this->load->library('email');
          $this->email->from($email, $name);
          $this->email->to($to); 
          $this->email->subject($object);
          $this->email->message($message);  
          $this->email->send();
      }
}

And this is the error showed when i try to send the mail:

An Error Was Encountered

The action you have requested is not allowed.

FIXED! the code was OK but i was passing the wrong variable in $this->email->to($interested); <- $interested go inside an if else and become $to ! that's why doesn't work! :D FIXED!

davidev
  • 314
  • 9
  • 22
  • Please post the solution as an answer below rather than in the question. However, since the problem was caused by a typo, IMO, I think you should delete it. Thank-you. – Sparky Feb 06 '15 at 18:53

2 Answers2

0

I've had this problem before and it's something to do with the CSRF security.

You should use the form helper instead of hard coding your forms.

https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

Once the helper is loaded your form tag would change, from this;

<form id="contact-form" class="wow bounceInUp" data-wow-offset="10" data-wow-delay="0.2s" method="POST" action="<?php echo site_url('landing/index'); ?>" onsubmit="return checkform();" />

To this;

<?php echo form_open('landing', 'class="wow bounceInUp" id="contact-form" data-wow-offset="10" data-wow-delay="0.2s" onsubmit="return checkform();"'); ?>

Edit...

<?php echo form_open('landing', 'id="contact-form" class="wow bounceInUp"  data-wow-offset="10" data-wow-delay="0.2s" onsubmit="return checkform();"'); ?>
Craig
  • 1,823
  • 1
  • 11
  • 12
  • ok i've added in the controller $this->load->helper('form'); and i've replaced my form declaration with yours.. but now the contact form disappears from the view ! – davidev Feb 06 '15 at 15:53
  • Do you have any CSS on the form? Try the second version of the form_open function I just posted, see if that one works... – Craig Feb 06 '15 at 15:58
  • same results.. don't load the form... i've to load something in some other parts in the config files in CodeIgniter?? – davidev Feb 06 '15 at 16:17
  • I've try using my first configuration, i've removed "index.php" from $config['index_page'] = ''; (application/config/config.php) And now when i send the mail i've this problem: No input file specified. – davidev Feb 06 '15 at 16:27
  • Does this help? http://stackoverflow.com/questions/6118740/codeigniter-no-input-file-specified – Craig Feb 06 '15 at 16:30
  • Load the form validation class. As well as the form helper. – Craig Feb 06 '15 at 16:36
  • OK I've fixed the .htaccess file thank you for your help but now the form return me this error: An Error Was Encountered The action you have requested is not allowed. what you mean for load the form validation class and as well the form helper ? – davidev Feb 06 '15 at 16:39
  • 1
    I was just reading about the CSRF protection. Apparently, it works much better when the form validation library is loaded. – Craig Feb 06 '15 at 16:45
  • i've fixed the code was OK but i was passing the wrong variable in $this->email->to($interested); <- $interested go inside an if else and become $to ! that's why doesn't work! :D FIXED! – davidev Feb 06 '15 at 18:18
-1

Set this Configuration in config.php files:

$config['csrf_protection'] = FALSE;  
Poobalan
  • 47
  • 7