[eluser]minoflow[/eluser]
Being somewhat of a CI and jQuery newb, I'm trying to snazz up a little email form with some ajax calls.
The problem I keep running into is that my validations and rules do not work. It will send the data just find (I'm getting the emails) but I'm not getting any of the validation errors or feedback which worked before I started adding the jQuery stuff.
Here is the code
Controller
Code:
<?php
class Contact extends Controller {
function Contact() {
parent::Controller();
$this->load->library('validation');
}
function index($data = array()) {
if (isset($_POST)) {
$rules['name'] = "trim|required|xss_clean";
$rules['email'] = "trim|required|valid_email";
$rules['message'] = "trim|required|xss_clean";
$this->validation->set_rules($rules);
$this->validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->validation->run() == TRUE) {
$this->load->library('email');
$this->email->from($this->input->post('email'), $this->input->post('name'));
$this->email->to('[email protected]');
$this->email->subject('Email from jaygiesen.com');
$this->email->message($this->input->post('message'));
if (!$this->email->send()) {
$data = array('feedback' => 'E-mail failed to send, try emailing me at [email protected]');
} else {
$data = array('feedback' => 'Thank you for your inquiry, I will get back to you as soon as possible.');
}
}
}
$data += array(
'title' => 'Jay Giesen : Contact',
'nav' => $this->nav->get_nav_data(),
'content_view' => 'contact',
);
$this->load->vars($data);
$this->load->view('container');
}
}
?>
View - Header with jQuery
Code:
<html>
<head>
<title><?=$title?></title>
$(document).ready(function() {
// handle the contact form ajax request.
$("#contact_form").submit(function() {
var form = $("#contact_form");
var action = form.attr("action");
var data = form.serialize();
$.post(action, data, function() {
alert("sucess");
});
return false;
});
jQuery().ajaxStart(function() {
alert("starting");
});
});
<link rel="stylesheet" href="<?=base_url()?>css/default.css" type="text/css" />
</head>
<body>
<a name="top"></a>
View - Form
Code:
<div id="content">
<p>This is my contact information.</p>
<?=$this->validation->error_string; ?>
<?php
if (isset($feedback)) {
echo "<div class=\"success\">$feedback</div><p>".anchor('/contact/', 'Send me another email')."</p>";
} else {
echo form_open('contact/', array('id' => 'contact_form'));
echo "<p>";
echo form_label('Your Name:', 'name');
echo form_input(array(
'name' => 'name',
'id' => 'name',
'maxlength' => '50',
'size' => '25',
'value' => $this->input->post('name')
));
echo "</p><p>";
echo form_label('Your Email:', 'email');
echo form_input(array(
'name' => 'email',
'id' => 'email',
'maxlength' => '50',
'size' => '25',
'value' => $this->input->post('email')
));
echo "</p><p>";
echo form_label('Message:', 'message');
echo form_textarea(array(
'name' => 'message',
'id' => 'message',
'rows' => '10',
'cols' => '45',
'value' => $this->input->post('message')
));
echo form_submit('submit', 'Send');
echo form_close();
}
?>
</div>
I would really appreciate any feedback, these forums have been a great tool for me to develop my CI skills.