I have written the following ajax code which validates my form which is actually a modal window. But after clicking close or save when I open the form again the values are still there. following is my code:
<script>
$(function() {
$("#submit_button").click(function( e ) {
e.preventDefault();
var name = $("input[name=name]").val();
var amount = $("input[name=amount]").val();
var model = $("select[name=model]").val();
var brand = $("select[name=brand]").val();
$.ajax({
type: "POST",
url: "<?=base_url()?>index.php/items/create",
cache: false,
dataType: "json",
data: 'name='+name+'&amount='+amount+'&model='+model+'&brand='+brand,
success: function(result){
if(result.error) {
$(".alert").fadeIn('slow');
$("#error_message").html(result.message);
} else {
$(".alert").fadeIn('slow');
$("#error_message").html(result.message);
$('#new_item').modal('hide')
}
}
});
});
});
</script>
This is the controller code:
public function create()
{
//creating the required validation fields
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('brand', 'Brand', 'trim|required');
$this->form_validation->set_rules('amount', 'Amount', 'trim|required|numeric');
$result = array();
if ($this->input->is_ajax_request())
{
if ($this->form_validation->run() == FALSE) {
$result['error'] = true;
$result['message'] = validation_errors();
} else {
$result['error'] = true;
$result['message'] = 'The record has been saved';
$this->m_items->create();
redirect('items/index', 'refresh');
}
$json = json_encode($result);
die($json);
}
else
{
redirect('items/index', 'refresh');
}
}
The second problem is that after i click save, the entry is made in the database but no message is displayed, the form stays at its place. Then I have to refresh the page manually and do it.
Can some one be kind enough and give me the solution…..i will be as much appreciative as any one can ever be!
Please help me out as I have been trying to fix it for more than 3 hours now!
Thank you in advance!
Because your not emptying them?