Delete file value with database information

Asked

Viewed 15 times

0

I have a registration form where between the fields has a file field. It registers normally.

<div class="form-group">
  <label for="foto">Foto do(a) Vereador(a):</label>
    <input type="file" name="Foto" class="form-control" id="fileUpload" placeholder="Foto do Vereador(a)">
    <img id="imagem" src="#" alt="Preview da sua imagem" style="width: 130px; margin-top: 10px; display: none" class="img-thumbnail" />
    <label id="excluir" style="margin-left: 50px; color: #00F; display: none; cursor: pointer">Excluir</label>
 </div>

Jquery

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $("#imagem").attr('src', e.target.result);           
        }     
        reader.readAsDataURL(input.files[0]);         
    }
}

$("#fileUpload").change(function(){
    readURL(this);
    $("#imagem").css("display","block");
    $("#excluir").css("display","block");
});

$("#excluir").click(function(){
    $("#fileUpload").val("");
    $("#imagem").css("display","none");
    $("#excluir").css("display","none");
});

All right, all right! The only problem is when I try to bring this field into user editing. At the time of bringing the results of the database to the issue I am doing so:

<div class="form-group">
  <label for="foto">Foto do(a) Vereador(a):</label>
    <input type="file" name="Foto" class="form-control" id="fileUpload" value="<?php echo $visualizar->FotoUsuario; ?>">
    <img id="imagem" src="<?php echo "../../fotos/".$visualizar->FotoUsuario; ?>" alt="Preview da sua imagem" style="width: 130px; margin-top: 10px;" class="img-thumbnail" /><br>
    <label id="excluir" style="margin-left: 50px; color: #00F; cursor: pointer">Excluir</label>
 </div>

The problem is when I click on the Delete link:

inserir a descrição da imagem aqui

It is not deleting the value from the file field. Jquery is the same as the one I passed above.

1 answer

1


Try to do with attr:

$("#excluir").click(function(){
    $("#fileUpload").removeAttr("value");
    $("#imagem, #excluir").css("display","none");
});
  • Hi Lucas. It didn’t work. The image name remains in the value of the file field.

  • Use removeAttr, try now

  • Thank you Lucas.

Browser other questions tagged

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