0
Everybody, good afternoon, everybody !
I’m setting up a local manager, and I’m having trouble searching the required fields, is returning Failed to load Resource: the server responded with a status of 500 (Internal Server Error)
jquery.js:9664 POST http://localhost/project/restrict/ajax_save_local 500 (Internal Server Error)
follows my code:
Form:
<div class="form-group">
<label class="col-lg-2 control-label">Local</label>
<div class="col-lg-10">
<input id="local_name" name="local_name" class="form-control" maxlength="100">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Imagem</label>
<div class="col-lg-10">
<img id="local_img_path" src="" style="max-height: 400px; max-width: 400px;">
<label class="btn btn-block btn-primary">
<i class="fa fa-upload"></i> Importar imagem
<input type="file" id="btn_upload_local_img" style="display: none;" accept="image/*">
</label>
<input id="local_img" name="local_img">
<span class="help-block"></span>
</div>
</div>
Function is_duplicated:
public function is_duplicated($field, $value, $id = NULL) {
if (!empty($id)) {
$this->db->where("locais_id <>", $id);
}
$this->db->from("locais");
$this->db->where($field, $value);
return $this->db->get()->num_rows() > 0;
}
Function ajax_save_places:
public function ajax_save_locais() {
if (!$this->input->is_ajax_request()) {
exit("Nenhum acesso de script direto permitido!");
}
$json = array();
$json["status"] = 1;
$json["error_list"] = array();
$this->load->model("local_model");
$data = $this->input->post();
if (empty($data["local_name"])) {
$json["error_list"]["#local_name"] = "Nome do local é obrigatório!";
} else {
if ($this->local_model->is_duplicated("local_name", $data["local_name"], $data["locais_id"])) {
$json["error_list"]["#local_name"] = "Nome do local já existente!";
}
}
if (!empty($json["error_list"])) {
$json["status"] = 0;
} else {
if (!empty($data["local_img"])) {
$file_name = basename($data["local_img"]);
$old_path = getcwd() . "/tmp/" . $file_name;
$new_path = getcwd() . "/public/images/locais/" . $file_name;
rename($old_path, $new_path);
$data["local_img"] = "/public/images/locais/" . $file_name;
}
if (empty($data["locais_id"])) {
$this->local_model->insert($data);
} else {
$locais_id = $data["locais_id"];
unset($data["locais_id"]);
$this->local_model->update($locais_id, $data);
}
}
echo json_encode($json);
Javascript:
function showErrorsModal(error_list) {
clearErrors();
$.each(error_list, function(id, message) {
$(id).parent().parent().addClass("has-error");
$(id).siblings(".help-block").html(message)
})
}
$.ajax({
type: "POST",
url: BASE_URL + "restrict/ajax_save_locais",
dataType: "json",
data: $(this).serialize(),
beforeSend: function() {
clearErrors();
$("#btn_save_locais").siblings(".help-block").html(loadingImg("Verificando..."));
},
success: function(response) {
clearErrors();
if (response["status"]) {
$("#modal_locais").modal("hide");
} else {
showErrorsModal(response["error_list"])
}
}
})
return false;
})