php does not find image input sent by Ajax

Asked

Viewed 32 times

2

made a javascript to send a picture of a form to php, the script does not return any error but when it arrives in php php can not find $_FILES[].

error: Exception has occurred. Notice: Undefined index: imagem

HTML

<form enctype="multipart/form-data"> 
<div class="input-field col s12">
        <i class="mdi-action-subject prefix"></i>
        <textarea id="infoadicional" type="text" name="infoadicional" maxlength=1000 class="materialize-textarea validate"></textarea>
        <label for="infoadicional">Informações Adicionais (ex: horário de atendimento, outros meios de contato)</label>
        <img id="img" src="#"  hidden="500px">
        <input type="file" id="imagem" onchange="readURL(this,'mini_foto_new');"><br><br>
        <input type="hidden" id="x" name="x" /><!--referente a posição do mouse-->
        <input type="hidden" id="y" name="y" />
        <input type="hidden" id="w" name="w" /><!-- referente a altura e largura da imagem-->
        <input type="hidden" id="h" name="h" />
    </div>
</form>

Knob

 <div class="modal-footer">
    <a href="#!" class="modal-close btn-flat">Cancelar</a>
    <button class="btn cyan" id="btn_criarAn" type="submit" form="formCriarAn">Criar</button>
    <script src="js/pegaimg.js" type="text/javascript"></script>
 </div>

Javascript

    document.getElementById("btn_criarAn").onclick=function(e){
        if(e.target.files != null && e.target.files.length !=0){
        var arquivo = e.target.files[0];
        var fd = new FormData();
        fd.append("imagem", arquivo);
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState===4&xmlhttp.status===200)
                alert(xmlhttp.responseText);    
        };
        xmlhttp.open("POST", "Chamadas/adicionarAn.php", true);
        xmlhttp.send(imagem);
    }
};

php

move_uploaded_file($_FILES["imagem"]["tmp_name"], $caminho);
  • See if this link helps you friend https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Usando_XMLHttpRequest

  • Young, whenever working with sending images, it is necessary to put the following in your form : enctype="multipart/form-data" so the form understands that you are working with images. It would look like this: <form enctype="multipart/form-data">

  • sorry friend I put this in the form but when I went to post I didn’t copy the copied the whole code and ended up putting the tags form after, I will edit

No answers

Browser other questions tagged

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