[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">
<?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);
?>
<br />
<?php
echo form_submit('submit', 'Send');
echo form_close();
?>
[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:"<?php echo base_url('contact/send_); ?>",
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:
<?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">• ','</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));
}
}