如何使html表单在codeigniter

时间:2016-05-12 09:39:09

标签: php jquery html codeigniter

我是codeigniter的新手,并制作了一个将所有细节发送到邮件的表单。 但我没有得到解决方案,我的表格不起作用。 这是我在视图中的视图文件

contact_us in view
<?php 
        $form=array('class'=>'contact-form default-form','id'=>'myform');
        echo form_open('success_form/success',$form);
        ?>
        <div class="row">

          <div class="col-md-12">
            <div class="preamble col-md-12">
              <h3>Get in touch</h3>
            </div>
          </div>
          <div class="col-md-6">
            <?php
            $name=array('placeholder'=>'Name','name'=>'name');
            echo form_input($name);
            ?>
          </div>
          <div class="col-md-6">
            <?php
            $company=array('placeholder'=>'company','name'=>'company');
            echo form_input($company);

            ?>
          </div>
          <div class="col-md-6">
            <?php
            $number=array('placeholder'=>'Phone Number','name'=>'number');
            echo form_input($number);
            ?>
          </div>
          <div class="col-md-6">
            <?php
            $email=array('placeholder'=>'Email','name'=>'email');
            echo form_input($email);
            ?>
          </div>
          <div class="col-md-6">
            <span class="arrival calendar">
            <?php
            $arrival=array('name'=>'arrival','placeholder'=>'Arrival','data-dateformat'=>'m/d/y');
            echo form_input($arrival);
            ?>
                <i class="fa fa-calendar"></i>
            </span>
          </div>
          <div class="col-md-6">
            <span class="departure calendar">
            <?php
            $departure=array('name'=>'departure','placeholder'=>'Departure','data-dateformat'=>'m/d/y');
            echo form_input($arrival);
            ?>
               <i class="fa fa-calendar"></i>
            </span>
          </div>
          <div class="col-md-12">

            <span class="select-box" title="hotel-type">
              <select name="person" data-placeholder="Number of People">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="10+">10+</option>
              </select>
            </span>
          </div>
          <div class="col-md-12">
            <textarea placeholder="How we can help you"></textarea>
          </div>
          <div class="col-lg-md text-center">
            <button class="btn btn-transparent-white">Send Message</button>
          </div>
        </div>
      <?php echo form_close();?>

这是我的控制器文件 控制器中的success_form

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

class Success_form extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->library(array('session', 'form_validation', 'email'));
    }

    public function index(){

        // Set Validation Rule

        $this->form_validation->set_rules('name','Name','trim|required|xss_clean|callback_alpha_space_only','required');
        $this->form_validation->set_rules('company','Company','trim|required|xss_clean|callback_alpha_space_only');
        $this->form_validation->set_rules('number','Mobile Number','required|regex_match[/^[0-9]{10}$/]');
        $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
        $this->form_validation->set_rules('arrival','Arrival','trim|required|xss_clean');
        $this->form_validation->set_rules('departure','Departure','trim|required|xss_clean');
        $this->form_validation->set_rules('person','Number of Person','trim|required|xss_clean|numeric');


    //Run Validation on Form Input

    if($this->form_validation->run()==FALSE){
     // Validation fails
        $this->load->view('contact_us_view');
    }
    else{
        // get the form data
        $name=$this->input->post('name');
        $company=$this->input->post('company');
        $number=$this->input->post('number');
        $email=$this->input->post('email');
        $arrival=$this->input->post('arrival');
        $departure=$this->input->post('departure');
        $person=$this->input->post('person');

        //Set email id
        $to_email='aziz.khan@vklalco.com';

        //configure email settings
            $config['protocol'] = 'smtp';
            $config['smtp_host'] = 'ssl://smtp.gmail.com';
            $config['smtp_port'] = '465';
            $config['smtp_user'] = 'user@gmail.com';
            $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($email, $name);
            $this->email->to('$to_email');
            $this->email->subject('testing mail');
            $this->email->message('content');

            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('success_form/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('success_form/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;
        }
    }

    function success(){
        $this->load->view('success_view');
    }

}

我尝试了很多,但没有得到答案。请帮助我找到解决方案..

1 个答案:

答案 0 :(得分:0)

在您的视图中,您正在向success_form控制器的成功操作提交表单。

  

echo form_open('success_form / success',$ form);

在您的成功动作中,您将再次加载相同的表单。

  

function success(){           $这 - &GT;负载&gt;查看( 'success_view');       }

您已在索引操作中编写了发送邮件逻辑,您需要在成功操作中调用该邮件以发送电子邮件。

function success(){
        $this->index();
        $this->load->view('success_view');
    }

这可能适合你。