Welcome Guest, Not a member yet? Register   Sign In
How to create a simple contact form within single controller site?
#1

[eluser]landitus[/eluser]
Hi, I'm building a simple single controller site where everything is contained within a "page" controller. Now I'm really stuck when trying to include a simple contact form that sends an email. I really don't know how to proceed with the single controller, how to manage the response an the validations. Can anyone help me?

This is the portion of the "page" controller that displays the contact form.

Code:
function contacto()
        {
    
        $this->load->helper('form');  
        $this->load->helper('email');
        $this->load->library('email');
                
          $vars['item'] = "contacto";
        $vars['page_title'] = "Contacto";
        $vars['menu_selected'] = $vars['item'];    
        $vars['css_class'] = $vars['item'];
        $vars['content_view'] = $vars['item'];
        $vars['uri1'] = $this->uri->segment(1);
          
            $this->load->view('template', $vars);
        
    }

There is a static view called "contacto" with this portion of code to render the form:

Code:
<?php echo validation_errors(); ?>
    <?php $attributes = array('class' => 'email', 'id' => 'myform'); ?>
    <?php echo form_open('contacto/enviar', $attributes); ?>
        <ul>
            <li>
                &lt;?php echo form_label('Nombre', 'nombre'); ?&gt;
                &lt;?php echo form_input('nombre') ;?&gt;
            </li>
            
            <li>
                &lt;?php echo form_label('Apellido', 'apellido'); ?&gt;
                &lt;?php echo form_input('apellido') ;?&gt;
            </li>
            
            <li>
                &lt;?php echo form_label('Email', 'email'); ?&gt;
                &lt;?php echo form_input('email') ;?&gt;
            </li>
            
            <li>
                &lt;?php echo form_label('Telefono', 'telefono'); ?&gt;
                &lt;?php echo form_input('telefono') ;?&gt;
            </li>
            
            <li>
                &lt;?php echo form_label('Comentario', 'comentario'); ?&gt;
                &lt;?php echo form_textarea('comentario') ;?&gt;
            </li>
            
        
        </ul>
    &lt;?php echo form_submit('mysubmit', 'Submit Post!'); ?&gt;
    
    
    &lt;/form&gt;
#2

[eluser]harman[/eluser]
use this Sample code in Your Controller

Code:
$subject=$this->input->post('subject');
                $msgbody=$this->input->post('msgbody');

                $rules = array(
                                array(
                                     'field'   => 'subject',
                                     'label'   => 'Subject',
                                     'rules'   => 'trim|xss_clean|encode_php_tags|required|min_length[5]|max_length[255]'
                                  ),
                                  array(
                                     'field'   => 'msgbody',
                                     'label'   => 'Message',
                                     'rules'   => 'trim|xss_clean|encode_php_tags|required|min_length[25]|max_length[900]'
                                  )
                               );

                $this->form_validation->set_rules($rules);
                if($this->form_validation->run()){

                       //Save Form Data Here (Call Your Save Function)


                }else{
                    //---Send Error Details
                    $this->session->set_flashdata('message',validation_errors());

                    $arrPostData=array('subject'=>$subject,'msgbody'=>$msgbody);
                    $this->session->set_flashdata('postdata',$arrPostData);

                    redirect(base_url()."msgbox/sendmsg/","refresh");
                }




Theme © iAndrew 2016 - Forum software by © MyBB