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

[eluser]LuckyFella73[/eluser]
Your js and controller code basically should look like this
(if I get you right on what you are going to achieve):
Code:
// your js
$(function() {
$('#form').submit(function (e) {
e.preventDefault();

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

$.ajax({
  url   : "<?php echo base_url('contact/send_email'); ?>",
  data  : form_data,
  type  : "post",
  dataType :"JSON",
  success: function(result) {
   if(result.validate == 1) {
    $('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);
   }
  }
})
});

}); // closing tag - document.ready


// Your Controller
public function send_email ()
{
// Don't load views here for you are only going to return a json result
// to your ajax function here

$result = array();
  
    $this->load->library('form_validation');


// [SET RULES ARE LOCATED HERE]
    // [ERROR DELIMITERS HERE]

    if ($this->form_validation->run() === FALSE)
{
  // Code here
  $result['validate'] = 0; // failed
}
else
{
       // Code here
    // email stuff
    
    $result['validate'] = 1; // succeeded
    }

die(json_encode($result));
}

Be sure your selector ( submit action ) matches


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