Welcome Guest, Not a member yet? Register   Sign In
Codeigniter AJAX validation email form
#1

[eluser]balboa_1990[/eluser]
After completing a php form using CI and the email class, i am able to receive html emails with the users data included-great. Now, as well as the CI validation, i would like to include client side validation (AJAX) with a nice fadeIn or fadeOut effect and still have CI validation running incase javascript is turned off. The code included here is what i have achieved so far from various sources and i know this is not complete but not even sure if im on the right tracks? So far with what i have the form still works fine and i assume its still running the CI validation script and there are no effects taken place. I would be gratfeul if someone could guide me to where i have went wrong so far and if possible what next steps to take? Here is the code to support my question:

NOTE: This is not my whole script, i've put in the relevant code and brackets what would be supporting it


VIEW
Code:
<div id="contact">
&lt;?php

$this->load->helper('form');

echo form_open('contact/send_email');

echo $success;

// empty div for error messages (php)
if(validation_errors() != ""){
    echo '<h3>Please correct the following errors:</h3>';
    echo '<div id="errors">';
    echo validation_errors();
    echo '</div>';
}


echo form_label('Email: ', 'email');
    $data = array (
        'name' => 'email',
        'id' => 'email',
        'value' =>set_value('email')
    );
echo form_input($data);

[I also have two more arrays for message and name, set out the same as email]


echo form_submit('submit', 'Send');

echo form_close();
?&gt;



AJAX

Code:
$(function() {
    $('form').click(function() {

        // get the form values
        var form_data = {
            name: $('name').val(),
            email: $('email').val(),
            message: $('message').val()
        };

        // send the form data to the controller
        $.ajax({
            url: "&lt;?php echo site_url('contact/send_email'); ?&gt;",
            type: "post",
            data: form_data,
            success: function(msg)
            {
            if(msg.validate)
                    {
                    $('form').prepend('<p>Message sent!</p>');
                    $('p').delay(3000).fadeOut(500);
                    }
                    else
                     $('form').prepend('<div id="errors">Message Error</div>');
                     $('p').delay(3000).fadeOut(500);
                }
            });
            return false;  
        });
    });
    [removed]


CONTROLLER
Code:
function __construct() {
    parent::__construct();
}

public function index()
{  
    $data['success'] = '';
    $data['page_title'] = 'Contact';
    $data['content'] = 'contact';
    $this->load->view('template', $data);
   }

public function send_email (){
    $this->load->library('form_validation');

    [SET RULES ARE LOCATED HERE]

    [ERROR DELIMITERS HERE]

    if ($this->form_validation->run() === FALSE) {
        $data['success'] = '';
        $data['page_title'] = 'Contact';
        $data['content'] = 'contact';
        $this->load->view('template', $data);  

    }else{
        $data['success'] = 'The email has successfully been sent';
        $data['name'] = $this->input->post('name');
        $data['email'] = $this->input->post('email');
        $data['message'] = $this->input->post('message');

        $html_email = $this->load->view('html_email', $data, true);

        //load the email class
        $this->load->library('email');

        $this->email->from(set_value('email'), set_value('name'));
        $this->email->to('-----EMAIL----');
        $this->email->subject('Message from Website');
        $this->email->message($html_email);

        $this->email->send();

        $data['page_title'] = 'Contact';
        $data['content'] = 'contact';  
        $this->load->view('template', $data);

        return null;
    }
}
}


Messages In This Thread
Codeigniter AJAX validation email form - by El Forum - 11-15-2012, 03:23 AM
Codeigniter AJAX validation email form - by El Forum - 11-15-2012, 03:52 AM
Codeigniter AJAX validation email form - by El Forum - 11-15-2012, 06:39 AM
Codeigniter AJAX validation email form - by El Forum - 11-15-2012, 10:20 AM
Codeigniter AJAX validation email form - by El Forum - 11-16-2012, 02:46 AM
Codeigniter AJAX validation email form - by El Forum - 11-16-2012, 03:18 AM
Codeigniter AJAX validation email form - by El Forum - 11-16-2012, 03:32 AM
Codeigniter AJAX validation email form - by El Forum - 11-16-2012, 03:40 AM
Codeigniter AJAX validation email form - by El Forum - 11-16-2012, 03:46 AM
Codeigniter AJAX validation email form - by El Forum - 11-16-2012, 04:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB