0

I'm getting this error when I try to update a file on my form..."You did not select a file to upload." ...and the problem is that I really am selecting a new file to upload...help me pls, I don't understand what is happening ...here is my code:

FORM

<form class="col s12" id="update_form" required="" enctype="multipart/form-data" aria-required="true" name="update_form" method="post" >
      <div class="row">
        <div class="input-field col s6">
          <input id="update_name" type="text" required="" aria-required="true" name="name" class="validate">
          <label for="first_name">Nombre</label>
        </div>
        <div class="input-field col s6">
          <input id="update_last_name" name="lastname" required="" aria-required="true" type="text" class="validate">
          <label for="last_name">Apellido</label>
        </div>
      </div>
      <div class="row">
        <div class="input-field col s6">
          <input id="update_side" type="text" required="" aria-required="true" name="side" class="validate">
          <label for="partido">Partido</label>
        </div>
        <div class="input-field col s6">
          <input id="update_charge" type="text" required="" aria-required="true" name="charge" class="validate">
          <label for="cargo">Cargo</label>
        </div>
      </div>
        <div class="row">
            <div class="input-field col s6">
               <div class="file-field input-field no-margin-top">
                  <div class="btn light-blue darken-4">
                    <span>Animación/Imagen</span>
                    <input type="file" name="animation">
                  </div>
                  <div class="file-path-wrapper">
                    <input class="file-path validate" id="animation" name="animation" type="text">
                  </div>
                </div>
            </div>

            <div class="input-field col s6">
            <select id="update_section" required="" aria-required="true" name="section" autocomplete="off">
              <option value="" disabled selected>Seleccione una opción</option>
              <option value="1">Presidencia</option>
              <option value="2">Senadores</option>
              <option value="3">Diputados</option>
            </select>
            <label>Sección</label>
          </div>
        </div>
        <input type="hidden" name="update_politic_hide" id="update_politic_hdn" value="">
    </form>

CONTROLLER

public function update_politic(){

  $this->load->model("politic");
  $params;

  if ($this->input->is_ajax_request()) {

    if (empty($this->input->post("animation"))){
        echo "first";
          $data = $this->politic->get_file_name($this->input->post("update_politic_hide"));
          $file = $data->POLITIC_FILE;//recupero el nombre de la imagen

          $params["name"] = $this->input->post("name");
          $params["lastname"] = $this->input->post("lastname");
          $params["side"] = $this->input->post("side");
          $params["charge"] = $this->input->post("charge");
          $params["section"] = $this->input->post("section");
          $params["animation"] = $file;
          $params["id"] = $this->input->post("update_politic_hide");

          if ($params["section"]=="Presidencia") {
              $params["section"]=1;
          }

          if ($params["section"]=="Senadores") {
              $params["section"]=2;
          }

          if ($params["section"]=="Diputados") {
              $params["section"]=3;
          }
    }
    else {
                  echo "second";
                $config['upload_path'] = "./public/uploads/";
                $config['allowed_types'] = "*";
                //$config['overwrite'] = "true";
                $config['max_size'] = "500000";
                $config['max_width'] = "2000";
                $config['max_height'] = "2000";

                $this->load->library('upload', $config);
                //$file = "animation";

                if (!$this->upload->do_upload("animation")) {
                  $data['uploadError'] = $this->upload->display_errors();
                  echo $this->upload->display_errors();
                }
          else {

                      $file_info = $this->upload->data();
                      $params["name"] = $this->input->post("name");
                      $params["lastname"] = $this->input->post("lastname");
                      $params["side"] = $this->input->post("side");
                      $params["charge"] = $this->input->post("charge");
                      $params["animation"] = $file_info['file_name'];
                      $params["section"] = $this->input->post("section");
                      $params["id"] = $this->input->post("update_politic_hide");

                      if ($params["section"]=="Presidencia") {
                          $params["section"]=1;
                      }

                      if ($params["section"]=="Senadores") {
                          $params["section"]=2;
                      }

                      if ($params["section"]=="Diputados") {
                          $params["section"]=3;
                      }

          }
        }

        $this->politic->update($params);
  }
}

MODEL

public function update($param){

  $id = $param["id"];
    $values = array(

                    "POLITIC_NAME" => $param["name"],
                    "POLITIC_LASTNAME" => $param["lastname"],
                    "POLITIC_SIDE" => $param["side"],
                    "POLITIC_CHARGE" => $param["charge"],
                    "POLITIC_FILE" => $param["animation"],
                    "SECTION_ID" => $param["section"],
                    );

  $this->db->where("POLITIC_ID",$id);
    $this->db->update("politics",$values);

}

JAVASCRIPT

$("#update_politic_btn").click(function(event) {
    /* Act on the event */

    var chango = $("#update_form").serialize();
    $.post(baseurl + 'admin/update_politic', chango,
        function(data) {
            console.log(data);
            list_politic();
        });
    event.preventDefault();
});


function update_politic(id, name, lastname, section, side, charge, file) {

    $("#update_politic_hdn").val(id);
    $("#update_name").val(name);
    $("#update_last_name").val(lastname);
    $("#update_side").val(side);
    $("#update_charge").val(charge);
    $('[name=section]').val(section);
    $("#animation").val(file);

    $('select').material_select();

}

2 Answers 2

1

There are two name attributes having the same value animation.

<input type="file" name="animation">

and

<input class="file-path validate" id="animation" name="animation" type="text">

The second name attribute is overriding the first one. You need to give it a different name.

Sign up to request clarification or add additional context in comments.

Comments

0

Change your Ajax code :

$("#update_politic_btn").click(function(event) {
var chango = new FormData($("#update_form")[0]);
// try this if above one not work
//var chango = new FormData($("#update_form"));
$.ajax({
    url: baseurl + 'admin/update_politic',
    type: 'POST',
    data: chango,
    async: false,
    success: function (data) {
        console.log(data);
        list_politic();
    },
    cache: false,
    contentType: false,
    processData: false,
    error : function() {

    }
});
event.preventDefault();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.