I need help with an elseif

2

I need help for this else if that does not register it well, I want to register an image in a modal but the one that validates it gives me error no sée if you need something special to validate it correctly because it gives me an error as if I would not have put anything.

          var_dump($_FILES['imagen']);
    if (empty($_POST['incidencia'])){
        $errors[] = "Por favor seleccione una incidencia";
    } elseif (empty($_POST['tema'])){
        $errors[] = "Tema vacío";
    }  elseif (empty($_POST['detalle'])) {
        $errors[] = "detalle vacío";
    } elseif (empty($_FILES['imagen']['name'])) {
        $errors[] = "imagen vacía";
    }  elseif (
        !empty($_POST['incidencia'])
        && !empty($_POST['tema'])
        && !empty($_POST['detalle'])
        && !empty($_FILES['imagen'])
    )

The html

                <?php
    if (isset($con))
    {
?>

<?php
    if (isset($con))
    {
?>

<div class="modal fade" id="myModal20" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><i class='glyphicon glyphicon-edit'></i> Agregar nueva incidencia</h4>
      </div>
      <div class="modal-body">
        <form class="form-horizontal" method="post" id="guardar_ticket" name="guardar_ticket" enctype="multipart/form-data">
        <div id="resultados_ajax"></div>
          <div class="form-group">
            <label for="incidencia" class="col-sm-3 control-label">Incidencia</label>
            <div class="col-sm-8">
              <input type="text" class="form-control" id="Incidencia" name="incidencia" placeholder="incidencia" required>
            </div>
          </div>
                      <div class="form-group">
            <label for="tema" class="col-sm-3 control-label">Asunto</label>
            <div class="col-sm-8">
              <input type="text" class="form-control" id="tema" name="tema" placeholder="Asunto" required>
            </div>
          </div>
          <div class="form-group">
            <label for="detalle" class="col-sm-3 control-label">Detalle</label>
            <div class="col-sm-8">
              <input type="text" class="form-control" id="detalle" name="detalle" placeholder="Detalle" required>
            </div>
          </div>
          <div class="form-group">
            <label for="imagen" class="col-sm-3 control-label">Imagen</label>
            <div class="col-sm-8">
              <input id="imagen" type="file" name="imagen" > 
            </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
        <button type="submit" class="btn btn-primary" id="guardar_datos">Guardar datos</button>
      </div>
      </form>
    </div>
  </div>
</div>
<?php
    }
?>

attached after doing the var_dump

I also attach the image of the DB

here the js

                  $(document).ready(function(){
        load(1);
    });

    function load(page){
        var q= $("#q").val();
        $("#loader").fadeIn('slow');
        $.ajax({
            url:'./ajax/buscar_ticket.php?action=ajax&page='+page+'&q='+q,
             beforeSend: function(objeto){
             $('#loader').html('<img src="./img/ajax-loader.gif"> Cargando...');
          },
            success:function(data){
                $(".outer_div").html(data).fadeIn('slow');
                $('#loader').html('');

            }
        })
    }



        function eliminar (Nticket)
    {
        var q= $("#q").val();
    if (confirm("Realmente deseas eliminar el la entrega")){    
    $.ajax({
    type: "GET",
    url: "./ajax/buscar_ticket.php",
    data: "Nticket="+Nticket,"q":q,
     beforeSend: function(objeto){
        $("#resultados").html("Mensaje: Cargando...");
      },
    success: function(datos){
    $("#resultados").html(datos);
    load(1);
    }
        });
    }
    }   

the other one

              $( "#guardar_ticket" ).submit(function( event ) {
            $('#guardar_datos').attr("disabled", true);

            var parametros = $(this).serialize();
             $.ajax({
            type: "POST",
        url: "ajax/nuevo_ticket.php",
        data: parametros,
         beforeSend: function(objeto){
            $("#resultados_ajax").html("Mensaje: Cargando...");
          },
        success: function(datos){
        $("#resultados_ajax").html(datos);
        $('#guardar_datos').attr("disabled", false);
        load(1);
              }
        });
           event.preventDefault();
          })
          $( "#editar_ticket" ).submit(function( event ) {
           $('#actualizar_datos2').attr("disabled", true);

        var parametros = $(this).serialize();
             $.ajax({
        type: "POST",
        url: "ajax/editar_ticket.php",
        data: parametros,
         beforeSend: function(objeto){
            $("#resultados_ajax2").html("Mensaje: Cargando...");
          },
        success: function(datos){
        $("#resultados_ajax2").html(datos);
        $('#actualizar_datos2').attr("disabled", false);
        load(1);
      }
        });
       event.preventDefault();
      })


        function get_user_ID(Nticket){
        $("#user_Nticket_mod").val(Nticket);
    }

        function obtener_datos(Nticket){
        var incidencia = $("#incidencia"+Nticket).val(Nticket);
        var tema = $("#tema"+Nticket).val(Nticket);
        var detalle = $("#detalle"+Nticket).val(Nticket);
        var imageni = $("imageni"+Nticket).val(Nticket);

        $("#mod_Nticket").val(Nticket);
        $("#incidencia").val(incidencia);
        $("#tema").val(tema);
        $("#detalle").val(detalle);
        $("#imageni").val(imageni);
    }

    
asked by Kevin Salazar 28.09.2017 в 18:50
source

2 answers

2

You have several errors:

  • The name of your input says image and you are capturing $_POST['imageni']

  • an image is captured using $_FILES['']

  • Your form tag must have enctype = 'multipart / form-data'

  • Try doing the condition like this:

    elseif (empty($_FILES['imagen']['name'])) {
    
    }
    
  • UPDATE:

    In your js you must do the following, as you are sending an image it is necessary to use a FormData (); as follows:

    $("#guardar_ticket").submit(function(e){
        e.preventDefault();
    
        var Incidencia = $("#Incidencia").val();
        var tema = $("#tema").val();
        var detalle = $("#detalle").val();
    
        var imagen = $("#imagen")[0].files[0];
    
        var parametros =  new FormData();
        parametros.append('Incidencia', Incidencia);
        parametros.append('tema', tema);
        parametros.append('detalle', detalle);
        parametros.append('imagen', imagen);
    
        $.ajax({
            type: "POST",
            url: "ajax/nuevo_ticket.php",
            data: parametros,
            cache: false,
            contentType: false,
            processData: false,
            beforeSend: function(objeto){
                $("#resultados_ajax").html("Mensaje: Cargando...");
            },
            success: function(datos){
                $("#resultados_ajax").html(datos);
                $('#guardar_datos').attr("disabled", false);
                load(1);
            }
        });
    
        /* Acá sigues con la lógica que tengas en esta función */
    })
    
    /* Sigues con la lógica de tu programa */
    
        
    answered by 28.09.2017 / 19:03
    source
    3

    I urge you that instead of nesting the if in that way do the following

    //Inicializo el arreglo de los errores (vacío)
    $errors = array();
    //Voy capturando los errores de 1 en 1
    if (empty($_POST['incidencia'])){
            $errors[] = "Por favor seleccione una incidencia";
        } 
    if (empty($_POST['tema'])){
            $errors[] = "Tema vacío";
        }  
    if (empty($_POST['detalle'])) {
            $errors[] = "detalle vacío";
        } 
    if (empty($_POST['imageni'])) {
            $errors[] = "imagen vacía";
    }
    //Si no hay ningún error
    if(count($errors) == 0){
       //Código a ejecutar cuando no hay errores
    }
    

    The problem of nesting them as you had was that if the first condition was fulfilled, it would not enter any other if, so if you had more than one empty field, you would only see the first one that you had conditioned in the If list. >     

    answered by 28.09.2017 в 19:06