Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
Data not sending to php JS/FormData/AJAX using PHP

I am trying to send a file to my DB together with text using this:


let formData = new FormData($('#image_form')[0]);
formData.append("message", $('#messageInput').val());

        $(document).ready(function() {
            $('#image_form').submit(function(e) {
                e.preventDefault();  
               $.ajax({  
                    url: "https://example.com/test.php",  
                    type: "POST",  
                    data: new FormData(this),  
                    contentType: false,  
                    processData:false,  
                    success: function(status) {
                        $('#result').append(status);
                        
                    }
                });
            });
        });

That's the DOM

<form id="image_form" enctype="multipart/form-data">
        <input type="file" name="files[]" id="files[]" multiple >
        <input type="submit" name="submit" is="submit" id="submit"/>
    </form>

<input type="text" name="message" id="messageInput">

PHP:

<?php 

$file = $_FILES['files'];
$message = $_POST['message'];

var_dump($_POST);

?>

The Image is received by php and can be display. But the Text is not.

Besides the Image, the php script outputs:

array(1) { ["submit"]=> string(6) "Submit" }
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You need to assign formData inside the submit handler, so it gets the values after the user has submitted the form.

$(document).ready(function() {
  $('#image_form').submit(function(e) {
    e.preventDefault();
    let formData = new FormData($('#image_form')[0]);
    formData.append("message", $('#messageInput').val());

    $.ajax({
      url: "https://example.com/test.php",
      type: "POST",
      data: formData,
      contentType: false,
      processData: false,
      success: function(status) {
        $('#result').append(status);

      }
    });
  });
});

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda