Welcome Guest, Not a member yet? Register   Sign In
Processing a form located in a common view file...
#1

[eluser]EBS Promo[/eluser]
I have a form that is in it's own view (global footer). The form is submitted to whatever page/controller it is displayed in.

What I did to make this work was puting all the logic in a helper file that holds a single function to validate the input and return a string variable for displaying in the view. Now doing it this way I have to load the helper, get the status, then pass that onto the footer view, all while keeping them in the same place.

Has anyone else figured out a better way to do this (not using AJAX)?

I know I can make a controller to handle the processing, but I would prefer to keep them on the same page the form was submitted from. I just am not sure if my solution is the most efficient to achieve the desired end result.

I Googled the $&%@ out of every term I could imagine to see if anyone else has discussed a similar issue before and didn't have any luck.
#2

[eluser]treeface[/eluser]
Hi EBS,

Could you perhaps post your code here? That'd help me (and undoubtedly others) get my mind around what you're trying to do.
#3

[eluser]EBS Promo[/eluser]
Here it is, I stripped out a lot of extra fields in the form and helper to simplify this example...

Controller:
Code:
class Home extends Controller {
    function Home()
    {
        parent::Controller();
    }    
    function index()
    {
        $this->load->view('header');
        $this->load->view('content');
        $this->load->helper('appointment_form');
        $data->appointment_form_status = appointment_form();
        $this->load->view('footer', $data);
    }    
}
Helper:
Code:
function appointment_form() {
    $CI =& get_instance();
    $CI->load->library('form_validation');
    $status = '';
    if ($CI->input->post('appointment_form')) {
        foreach ($_POST as $key => $value) {
            if (is_array($_POST[$key])) $key .= '[]';
            $CI->form_validation->set_rules($key, '', '');
        }
        $CI->form_validation->set_rules('full_name', 'Your Full Name', 'required|min_length[5]');
        $CI->form_validation->set_rules('email_address', 'Email Address', 'valid_email|strtolower');
        if ($CI->form_validation->run() == FALSE) {
            $status = '<div class="appointmentFormError"><p><strong>Whoops!</strong></p>' . validation_errors() . '</div>';
            $CI->form_validation->set_error_delimiters('<div class="appointmentError">', '</div>');
        } else {
            $CI->load->library('email');
            $CI->email->from($CI->input->post('email_address'), $CI->input->post('full_name'));
            $CI->email->to('[email protected]');
            $CI->email->subject("Appointment Request from {$_SERVER['HTTP_HOST']}");
            $CI->email->message("Full Name: {$_POST['full_name']}\nPhone Number: {$_POST['phone_number']}\nEmail Address: {$_POST['email_address']}\n\n{$_POST['comments']}");
            $CI->email->send();
            $status = '<div class="appointmentFormSuccess"><p><strong>Thank You!</strong></p><p>Your request to schedule an appointment has been submitted!</p></div>';
        }
    }
    return $status;
}
View (form):
Code:
&lt;?=$appointment_form_status?&gt;
    <div id="appointmentWrapper">
        &lt;form method="post" action="&lt;?=$this-&gt;uri->uri_string()?&gt;#appointmentWrapper" onsubmit="document.getElementById('loadingIndicator').style.display='block'">
            <div id="appointmentFormContactInfo">
                &lt;input name="full_name" type="text" value="&lt;?=set_value('full_name', 'Your Full Name')?&gt;"&gt;&lt;br />
                &lt;input name="phone_number" type="text" value="&lt;?=set_value('phone_number', 'Phone Number')?&gt;"&gt;&lt;br />
                &lt;input name="email_address" type="text" value="&lt;?=set_value('email_address', 'Email Address')?&gt;"&gt;&lt;br />
            </div>
            <div id="appointmentFormComments">
                &lt;textarea name="comments" cols="20" rows="5" style="width: 190px; height: 95px; margin-bottom: 2px"&gt;&lt;?=set_value('comments', 'Comments & Concerns')?&gt;&lt;/textarea&gt;&lt;br />
                &lt;input type="image" src="images/submit.gif" alt="Submit" style="float: right" /&gt;
                &lt;input type="hidden" name="appointment_form" value="1" /&gt;&lt;/div>
        &lt;/form&gt;
    </div>
View (footer):
Code:
</div>
    &lt;?php $this->load->view('appointment_form'); ?&gt;
    <div id="pageFooter">
        Copyright &lt;?=date('Y')?&gt;</div>
</div>
&lt;/body&gt;
&lt;/html&gt;




Theme © iAndrew 2016 - Forum software by © MyBB