I have a working submit form using ajax and json.
How ever my question is how can I use redirect() propely when form success and make sure it redirects to my dashboard.
Currently even though form submitted success it will not redirect.
I check my fire bug and it is sending it as get?
View Ajax
How ever my question is how can I use redirect() propely when form success and make sure it redirects to my dashboard.
Currently even though form submitted success it will not redirect.
I check my fire bug and it is sending it as get?
Code:
GET http://localhost/forum/admin/common/dashboard 200 OK 54ms
PHP Code:
public function verify() {
$data = array('success' => false, 'messages' => array());
$this->form_validation->set_rules('username', 'username', 'trim|required');
$this->form_validation->set_rules('password', 'password', 'trim|required');
$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
$this->form_validation->set_message('required', 'Your {field} is empty. Please type your {field} in.');
if ($this->form_validation->run() == false) {
foreach ($_POST as $key => $value) {
$data['messages'][$key] = form_error($key);
}
} else {
$data['success'] = true;
redirect('admin/common/dashboard');
}
echo json_encode($data);
}
View Ajax
Code:
<script type="text/javascript">
$('#form-login').submit(function(e) {
e.preventDefault();
var info = $(this);
$.ajax({
url: info.attr('action'),
type: 'post',
data: info.serialize(),
dataType: 'json',
success: function(response) {
if (response.success == true) {
$('.form-group').removeClass('has-error')
.removeClass('has-success');
$('.text-danger').remove();
} else {
$.each(response.messages, function(key, value){
var element = $('#' + key);
element.closest('div.form-group')
.removeClass('has-error')
.addClass(value.length > 0 ? 'has-error' : 'has-success')
.find('.text-danger').remove();
element.after(value);
});
}
}
});
});
</script>