$
(
"#createStudentForm"
).
unbind
(
'submit'
).
bind
(
'submit'
,
function
() {
var
form
=
$
(
this
);
var
formData
=
new
FormData
(
$
(
this
)[
0
]);
var
url
=
form
.
attr
(
'action'
);
var
type
=
form
.
attr
(
'method'
);
$
.
ajax
({
url :
url
,
type :
type
,
data:
formData
,
dataType:
'json'
,
cache:
false
,
contentType:
false
,
processData:
false
,
async:
false
,
success
:
function
(
response
) {
var
data
=
JSON
.
parse
(
response
);
if
(
response
.
success
==
true
) {
$
(
"#add-student-messages"
).
html
(
'<div class="alert alert-success alert-dismissible" role="alert">'
+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'
+
response
.
messages
+
'</div>'
);
manageStudentTable
.
ajax
.
reload
(
null
,
false
);
$
(
'.form-group'
).
removeClass
(
'has-error'
).
removeClass
(
'has-success'
);
$
(
'.text-danger'
).
remove
();
clearForm
();
}
else
{
if
(
response
.
messages
instanceof
Object
) {
$
.
each
(
response
.
messages
,
function
(
index
,
value
) {
var
key
=
$
(
"#"
+
index
);
key
.
closest
(
'.form-group'
)
.
removeClass
(
'has-error'
)
.
removeClass
(
'has-success'
)
.
addClass
(
value
.
length
>
0
?
'has-error'
:
'has-success'
)
.
find
(
'.text-danger'
).
remove
();
key
.
after
(
value
);
});
}
else
{
$
(
"#add-student-messages"
).
html
(
'<div class="alert alert-warning alert-dismissible" role="alert">'
+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'
+
response
.
messages
+
'</div>'
);
}
}
// /else
}
// /success
});
// /ajax
return
false
;
});
});