I can not save the image in the path of the error folder: Notice: Undefined index: photo in C

0

I try to save the image in a directory outside this directory called img , it happens that it does not save and it sends me the same error I can not find how I save the image. You can help me visualize the error, in the following code.

registry_categoria.php

    <?php
    if (isset($con))
    {
?>
<!-- Modal -->
<div class="modal fade" id="nuevoCliente" 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 nuevo catalogo</h4>
      </div>
      <div class="modal-body">
      <form class="form-horizontal" method="post" id="guardar_categoria" name="guardar_categoria" enctype="multipart/form-data">
        <div id="resultados_ajax"></div>

         <div class="form-group">
            <label for="foto" class="col-sm-3 control-label">Foto</label>
            <div class="col-sm-8">
              <input type="file" class="form-control" id="foto" name="foto">
            </div>
          </div>

          <div class="form-group">
            <label for="nombre" class="col-sm-3 control-label">Nombre</label>
            <div class="col-sm-8">
              <input type="text" class="form-control" id="nombre" name="nombre" required>
            </div>
          </div>


          <div class="form-group">
            <label for="descripcion" class="col-sm-3 control-label">Descripción</label>
            <div class="col-sm-8">
                <textarea class="form-control" id="descripcion" name="descripcion"   maxlength="255" ></textarea>

            </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_categoria">Guardar datos</button>
      </div>
      </form>
    </div>
  </div>
</div>
<?php
    }
?>

categories.js

$( "#guardar_categoria" ).submit(guardar_categoria) 

function insertarImagen(evento){
    evento.preventDefault()
    var datos = new FormData($("#guardar_categoria")[0])
    $("resultados_ajax").html("Mensaje: Cargando...")
    $.ajax({
        url: 'ajax/nueva_categoria.php',
        type: 'POST',
        data: datos,
        contentType: false,
        processData: false,
        success: function(datos){
            $("#resultados_ajax").html(datos)
        }
    })
}

new_category.php

<?php
include('is_logged.php');//Archivo verifica que el usario que intenta acceder a la URL esta logueado
/*Inicia validacion del lado del servidor*/

if(empty($_POST['nombre'])) {
       $errors[] = "Nombre vacío";  
    } else if (!empty($_POST['nombre'])){
    /* Connect To Database*/
    require_once ("../config/db.php");//Contiene las variables de configuracion para conectar a la base de datos
    require_once ("../config/conexion.php");//Contiene funcion que conecta a la base de datos
    // escaping, additionally removing everything that could be (html/javascript-) code


    $nombre=mysqli_real_escape_string($con,(strip_tags($_POST["nombre"],ENT_QUOTES)));
    $descripcion=mysqli_real_escape_string($con,(strip_tags($_POST["descripcion"],ENT_QUOTES)));                  
    $date_added=date("Y-m-d H:i:s");
    $foto=$_FILES["foto"];
        if ($foto["type"] == "img/jpg" OR $foto["type"] == "img/jpeg" ) {
            $ruta = "img/".md5($foto["tmp_name"]).".jpg";        
    $sql="INSERT INTO categorias (foto, nombre_categoria, descripcion_categoria,date_added) VALUES ('$ruta','$nombre','$descripcion','$date_added')";
    $query_new_insert = mysqli_query($con,$sql);
        if ($query_new_insert){
            move_uploaded_file($foto["tmp_name"], $ruta);
            echo "El catalogo ha sido ingresada satisfactoriamente.";
        } else{
            $errors []= "Lo siento algo ha salido mal intenta nuevamente.".mysqli_error($con);
        }
    } else {
        $errors []= "Error desconocido.";
    }
    }
    if (isset($errors)){

        ?>
        <div class="alert alert-danger" role="alert">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
                <strong>Error!</strong> 
                <?php
                    foreach ($errors as $error) {
                            echo $error;
                        }

                    ?>
        </div>
        <?php
        }
        if (isset($messages)){

            ?>
            <div class="alert alert-success" role="alert">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <strong>¡Bien hecho!</strong>
                    <?php
                        foreach ($messages as $message) {
                                echo $message;
                            }
                        ?>
            </div>
            <?php
        }

? >

    
asked by Jeinner Marulanda 19.07.2017 в 22:14
source

2 answers

0

Hi, check your code and I found some errors as duplicate of the id ' guardar_categoria ' in the html, you should not give the same id to the form and button (the button is not a form) and where it is defined the function you call in this line $( "#guardar_categoria" ).submit(guardar_categoria) deverias call the function insertarImagen , correct what I tell you and tell me if you find greetings

    
answered by 20.07.2017 в 15:57
0

kevin thanks for your help, but it generates the same error: change the id of the submit button to "save_data" and in the js:
                                                            $ (

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

    var datos = new FormData($("#guardar_categoria")[0])
    $("resultados_ajax").html("Mensaje: Cargando...")
    $.ajax({
        url: 'ajax/nueva_categoria.php',
        type: 'POST',
        data: datos,
        contentType: false,
        processData: false,
        success: function(datos){
            $("#resultados_ajax").html(datos);
            $('#guardar_datos').attr("disabled", false);
            load(1);
        }
    });
    event.preventDefault()
}) 

and the submit button stayed with id = save_data <button type="submit" class="btn btn-primary" id="guardar_datos">Guardar datos</button>

but if the same error comes:

  

Notice: Undefined index: photo in   C: \ xampp \ htdocs \ stock \ ajax \ new_categoria.php on line 17

    
answered by 20.07.2017 в 17:33