0
I have a webcam script inside a form and I need that when I take the photo, it saves the file with the name typed in the input in the same form.
This is the part of the webcam code where it sends a php file to save the photo
var nome = document.getElementById("nome").value;
document.getElementById("save").addEventListener("click", function() {
$.post('library/salvar.php?nome='.nome, {imagem:canvas.toDataURL()}, function(data){
},'json');
});
PHP file that receives the photo and saves
function base64_to_jpeg( $base64_string, $output_file ) {
$ifp = fopen( $output_file, "wb" );
fwrite( $ifp, base64_decode( $base64_string) );
fclose( $ifp );
return( $output_file );
}
$imagem = str_replace('data:image/png;base64,','',$_POST['imagem']);
base64_to_jpeg($imagem, "../assets/fotos/".$_GET['nome'].".png");
echo json_encode(array('imagem' => 1));
But he’s not saving you, I think I’m doing it wrong:
'library/salvar.php?nome='.nome,
Please help me out!!!
SOLVED! Replaces the line
$.post('library/salvar.php?nome='.nome, {imagem:canvas.toDataURL()}, function(data){
For
$.post('library/salvar.php?nome='+$("input[name=nome]").val(), {imagem:canvas.toDataURL()}, function(data){
To concatenate strings in javascript, it is necessary to use the signal + instead of the point.
$.post('library/salvar.php?nome='+nome
– Valdeir Psr
Very well remembered friend @Valdeirpsr, but now he saves the photo only without name..
– Nicholas Ruschel Krüger
Solved friend... fiucou thus $.post('library/save.php? name='+$("input[name=name]"). val() and managed to save with the name! thanks my friend!
– Nicholas Ruschel Krüger