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

[eluser]balboa_1990[/eluser]
LuckyFella73- Thank you, i knew it was something to do in the controller, I am nearly there and with your snippets the following occurs:

Leave the Form blank: Error message appears and fades-fab
Fill in the form correctly: Error message appears however the email appears in my inbox (so it knows its successful).

I am not sure if I've positioned the result array in my controller or if the "if else" statement i have them mixed up? Ive added new div id the selectors so i can see if there is any change but it looks like it repeats the error selector. Here is my code below for the overall structure from your advice:

Note: not sure if i need to add dataType:html as I'm sending a html email (html view is loaded in the controller).

Thanks!

VIEW
Code:
<h1>Contact</h1>
<div id="contact">
&lt;?php
//This is loaded in the view as it wont be used in the other pages
$this->load->helper('form');

echo form_open('contact/send_);

//success message if passed validation checks
echo '<div id="success">';
echo $success;
echo '</div>';

// empty div for error messages (ajax)
echo '<div id="errors">';

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

echo '</div>';

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


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


echo form_label('Message: ', ');
    $data = array (
        ' => ',
        'id' => ',
        'value' =>set_value(')
    );
echo form_textarea($data);
?&gt;

<br />

&lt;?php
echo form_submit('submit', 'Send');

echo form_close();
?&gt;

[removed]
$(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:"&lt;?php echo base_url('contact/send_); ?&gt;",
    data:form_data,
    type:"post",
    dataType :"JSON",
    success: function(result) {
    
  if(result.validate == 1) {
   $('form').prepend('<div id="success">Message Successful</div>');
   $('#success').fadeOut(1000);
  }else{
   $('form').prepend('<div id="errors">Message Error</div>');
   $('#errors').fadeOut(1000);
      }
    }
  })
});
}); // closing tag - document.ready
[removed]

</div>

CONTROLLER
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends CI_Controller {

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 (){
  
  $result = array();
  
  $this->load->library(');
  
  $this->form_validation->set_rules(','Name','trim|required|htmlspecialchars|alpha|max_length[30]|xss_clean');
   $this->form_validation->set_rules(','Email Address','trim|valid_email|required|htmlspecialchars|max_length[75]|xss_clean');
  $this->form_validation->set_rules(','Message','trim|required|htmlspecialchars|xss_clean');
  
  $this->form_validation->set_error_delimiters('<div id="errors">&bull;&nbsp;','</div>');
  
  if ($this->form_validation->run() == FALSE) {
  
   $result['validate'] = 0; // failed
  
   $data['success'] = '';
   $data['page_title'] = 'Contact';
   $data['content'] = 'contact';
   $this->load->view('template', $data);


  }else{
   $data['success'] = 'The email has successfully been sent';
   $data['] = $this->input->post(');
   $data['] = $this->input->post(');
   $data['] = $this->input->post(');
  
   $html_email = $this->load->view('html_email', $data, true);

   //load the email class
   $this->load->library(');
  
   $this->email->from(set_value('), set_value('));
   $this->email->to('email-address-removed');
   $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);
  
   $result['validate'] = 1; // succeeded
  
        }
        die(json_encode($result));
   }
}


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