Send file together with data in AJAX

Asked

Viewed 24 times

0

Hello, I want to send a file in the form, along with other inputs via AJAX, so there is no refresh on the page. However, when sending the form, nor the file, as the data is coming in MYSQL, just sending a new ID.

Below is my AJAX, where I cannot identify the error, I have tried several ways.

   jQuery(function($) {
  $("#form_video").on("submit", function(e) {
    e.preventDefault(); // impedir o evento submit

    let form = new FormData();
    form.append("arquivo", $("#arquivo").val());
    form.append("file", $('#arquivo').prop('files')[0]);

    var nome_video = $("#nome_video").val();
    var disciplina = $("#disciplina").val();
    var link_video = $("#link_video").val();
    var nomecomp = $("#nomecomp").val();
    var coment_video = $("#coment_video").val();

    $.ajax({
      url: "registrar_video.php",
      type: "POST",
      data: {
        form
      },
      cache: false,
      processData: false,
      
      success: function(data){
        $('#video_enviado').removeClass("alert alert-success d-none");
        $('#video_enviado').addClass("alert alert-success d-block");
      },
      error: function(data){
        $('#video_n_enviado').removeClass("alert alert-danger d-none");
        $('#video_n_enviado').addClass("alert alert-danger d-block");
      }
    });


  });
});

Also, my file registrar_video.php

 <?php
include("conexao.php");

$nome_video = $_POST['nome_video'];
$disciplina  = $_POST['disciplina'];
//reduzir link do vídeo
$url = $_POST['link_video'];
parse_str(parse_url($url, PHP_URL_QUERY), $saida_de_variaveis);
$link_video = $saida_de_variaveis['v'];

$monitor = $_POST["nomecomp"];
$coment_video  = $_POST['coment_video'];
//pegar arquivo
$arq = $_FILES['arquivo']['name'];

if (is_uploaded_file($_FILES['arquivo']['tmp_name'])) {

  $arq = str_replace(" ", "_", $arq);
  $arq = str_replace("ç", "c", $arq);

  if (file_exists("upload/$arq")) { //aa
    $a = 1;
    while (file_exists("upload/[$a]$arq")) { //aa
      $a++;
    }
    $arq = "[" . $a . "]" . $arq; //aa
  }
  if (move_uploaded_file($_FILES['arquivo']['tmp_name'], "upload/" . $arq)) {

    $sql_logar = "INSERT INTO video_monitor (titulo_video, disciplina, link_video, coment_video, arquivo, data, monitor, pendente, situacao) 
      VALUES('$nome_video', '$disciplina', '$link_video', '$coment_video', '$arq', NOW(), '$monitor', '1', 'Aguardando análise pelo professor!')";
    $exe_logar = mysqli_query($conection, $sql_logar) or die(mysqli_error($conection));

  }
} else {

  $sql_logar = "INSERT INTO video_monitor (titulo_video, disciplina, link_video, coment_video, arquivo, data, monitor, pendente, situacao)
      VALUES ('$nome_video', '$disciplina', '$link_video', '$coment_video', 'Nenhum arquivo', NOW(), '$monitor', '1', 'Aguardando análise pelo professor!')";
  $exe_logar = mysqli_query($conection, $sql_logar) or die(mysqli_error($conection));
}

I tried to do it by form without Jquery, but it reloads the page, so it was working.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.