1
I’m trying to upload without From, for image upload, but I can’t.
html
<input id="avatar" type="file" name="avatar" />
<button id="btnOcorrenciaSalvar">Salvar</button>
Javascript code:
$("#btnOcorrenciaSalvar").click(function() {
event.preventDefault();
var form_data = new FormData();
nomeFoto = event.filename;
var file_data = $("#avatar").prop("files")[0];
form_data.append("file", file_data)
form_data.append("foto", nomeFoto)
$.ajax({
url: 'http://url.com.br/temp.php',
type: 'POST',
data: form_data,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
//$("#productFormOutput").html(returndata);
alert(form_data);
},
error: function () {
alert("error in ajax form submission");
}
});
return false;
});
PHP code to receive the POST:
<?php
move_uploaded_file($_FILES["avatar"]["tmp_name"], "ocorrencias/" . $_POST["foto"]);
?>
Good morning. This error appears
Uncaught TypeError: undefined is not a function
. on the line where the$('#formulario').ajaxForm
. What is this?– Tiago
in place of #form should be the id of your form!
– Victor Hugo