I am using codeigniter 2.1.2 and Bootstrap.
I am opening a form in a modal window.
The form is inside a div with id status.
The form has a few input fields and an upload file field.
I am trying to submit the form with ajaxForm.
In my controller I echo "success" if everything is fine.
public function edit($provinceId, $bookId, $id = NULL)
{
$province = $this->province_m->get($provinceId);
$book = $this->book_m->get($bookId);
if($id)
{
$this->data['issue'] = $this->issue_m->get($id);
count($this->data['issue']) || $this->data['errors'][] = 'The issue could not be found.';
}
else
{
$this->data['issue'] = $this->issue_m->get_new();
}
// Set up the form
$rules = $this->issue_m->rules;
$this->form_validation->set_rules($rules);
// Checks if issue save was pressed, otherwise it will show the errors becuase of the post of province and book
if($this->input->post('issueposted') != '')
{
if ($this->form_validation->run() == TRUE)
{
$data = $this->issue_m->array_from_post(array('mag_folder'));
$data['province_id'] = $provinceId;
$data['book_id'] = $bookId;
$data['publish_date'] = mdate('%Y-%m-%d', strtotime($this->input->post('publish_date')));
if($_FILES['image']['error'] === 0)
{
$uploaded = $this->do_upload($province->folder, $book->name);
if($uploaded)
{
$data['image'] = $this->upload->file_name;
$this->issue_m->save($data, $id);
echo "success";
}
}
else //No file to upload
{
$this->issue_m->save($data, $id);
echo "success";
}
}
}
// Load the view
$this->data['url'] = site_url('admin/issue/edit') . '/' . $province->id . '/' . $book->id . '/' . $id;
$this->data['province'] = $province;
$this->data['book'] = $book;
$data = $this->load->view('admin/issue/edit', $this->data, TRUE);
$this->output->set_output($data);
}
}
The javascript:
$(function() {
var options = {
target: '#status',
success: function(data) {
alert(data);
window.location.replace("http://thinklocal.dev/admin/province");
}
};
// pass options to ajaxForm
$('#issueform').ajaxForm(options);
});
Everything worked fine besides the fact that the page was not redirecting on success.
When I alert the data I get this: success<div id="status">
I also tried this way, but here when I click the submit button, nothing happens.
$('#issueform').submit( function()
{
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
$(this).ajaxForm({
beforeSend: function() {
status.html();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
success: function(xhr) {
status.html(xhr.responseText);
}
return false;
});
});
Can someone please help me figure out how do I submit the form and redirect if there are no errors, or go back to the modal window if there are errors?