[eluser]LuckyFella73[/eluser]
I tested the code and realized that you have to load the form_validation
class in your callback function and that your form-tag has to be removed.
Here the complete code, tested:
view css:
Code:
<style type="text/css">
div.msg-hide {display:none;}
div.msg-show {display:block;}
</style>
view form:
Code:
Full Name:<br />
<?php
$attrib = array('id' => 'fname', 'name' => 'fname');
echo form_input($attrib).form_error('fname');
?>
<br />E-mail Address:<br />
<?php
$attrib = array('id' => 'email', 'name' => 'email');
echo form_input($attrib).form_error('email');
?>
<br /><br />
<?php
$attrib = array('id' => 'submit_item', 'name' => 'submit_item', 'value' => 'Add Person');
echo form_submit($attrib);
echo form_reset('reset', 'Reset Fields');
echo form_close(); ?>
global . js:
Code:
$(document).ready(function()
{
$("#submit_item").click(function ()
{
var fname = $('#fname').val();
var email = $('#email').val();
$.post("form/add_person", { "fname" : fname, "email" : email },
function(data)
{
$('#box_message').removeClass("msg-hide");
$('#box_message').addClass("msg-show");
$("#box_message").html(data.message);
}, 'json');
});
});
controller:
Code:
<?php
class Form extends Controller {
function Form()
{
parent::Controller();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
}
function index()
{
$this->load->view('form_view');
}
function add_person()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('fname', ' ', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', ' ', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$array = '{ "message" : "Input not valid" }';
echo $array;
}
else
{
#$this->load->model('Form_model', '', TRUE);
#$this->Form_model->add_person();
$array = '{ "message" : "All data has been saved" }';
echo $array;
}
}
}
/* End of file form.php */
/* Location: ./system/application/controllers/form.php */
Don't forget to uncomment the 2 model-calls I had to deactivate ..