[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">
<?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();
?>
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: "<?php echo site_url('contact/send_email'); ?>",
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;
}
}
}