Send image with ajax

Asked

Viewed 71 times

4

I am trying to receive a PHP image sent through AJAX, but I was not successful. Follow the code:

jQuery:

$("#envmsg").submit(function() {
    $("#btn-enviarmsg").button("loading");
    $("body").css("cursor", "wait");

    var dados = $(this).serialize();

    $.ajax({
        type: "POST",
        url: "/enviamsg.php",
        data: dados,
        success: function(data) {
            document.getElementById("envmsg").reset();
            $("#btn-enviarmsg").button("reset");
            $("body").css("cursor", "default");
            $("#deixarMensagem").modal("hide");
            location.reload();
        }
    });

    return false;
});

and in the enviar.php. i do the checking:

if($_FILES["foto"]) {
        $foto = echo "<script>alert('tem foto');</script>";
    } else {
        $foto = echo "<script>alert('n tem foto');</script>";
    }

But nothing occore, why?

Note: This form sends some text fields and the image. I just posted snippets of the code related to the problem.

Note: My form is enctype="Multipart/form-data".

Thank you!

  • 2

    Would that not be the same as this question? http://answall.com/questions/29770/

  • 1

    Opa, thank you @Bacco, I had done several searches, but I had not seen the link you gave me. Well, it worked. Thanks.

  • Anyway, your question will link to each other, increasing the chance of someone with the same problem finding the solution when searching.

No answers

Browser other questions tagged

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