1
Well, I have a form that has a input
of type ='file'
. I’ve seen on several sites and examples of how to do and all that I test do not work. the code can not share more is more or less like this.
I have a form with input file and an input button on the button the JS calls a function that takes the values
of form
and makes a array(JSON)
, then do Ajax with PHP for insertion in the database.
Only that the array when this in PHO does not come the name of the image. Blz so I did direct in PHP like this: $img = $_FILES["arquivo"]["name"];
and it also returns the variable empty. All is correct form
with the enctype, more anyway does not work.
Html:
<form action="?pagina=cadastroEmpresas" method="POST" id="formEmpresa" name="formEmpresa" enctype="multipart/form-data">
<input value="Salvar" id="btnSalvarForm" style="padding: 5px; font-size: 14px; margin-left: -13px;" type="button">
<label for="arquivo">Logo:</label> <input name="arquivo" id="arquivo" value="" type="file" />
JS:
$( "#btnSalvarForm" ).on("click", function() {
inserirEmpresa();
});
function inserirEmpresa(){
campos = pegarCampos( "formEmpresa" );
$.ajax({
type : "POST",
data : {dados:JSON.stringify(campos)},
url : "php/buscaEmpresasBD.php?escolha=7",
success : function(resposta){
alert(resposta);
$("#divCamposForm").css("display", "none");
}//fim success
});//fim ajax
}
PHP:
function inserirEmpresa(){
global $cnx;
//echo $_POST['dados'];
$dados = json_decode($_POST['dados'], TRUE);
//echo $dados["txtNomeFantasiaEmpresa"];
$target_dir = "galeria/";
//$target_file = $target_dir . basename($_FILES["arquivo"]["name"]);
$target_file = $target_dir . basename($_FILES["arquivo"]["name"]);
echo "$target_file";
}
So without giving more data or showing the code it is difficult to help.
– Jorge B.
If everything is correct the problem must be something else, put the code you have.
– rray
The
input[type=file]
submits only the byte array that represents the local physical file. If you want to submit the file name you need to assign byJavaScript
to anotherinput
the name of the selected file displayed ininput[typr=file]
.– MFedatto