Take the value of a JS input to send to PHP file

Asked

Viewed 131 times

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){
  • 2

    To concatenate strings in javascript, it is necessary to use the signal + instead of the point. $.post('library/salvar.php?nome='+nome

  • Very well remembered friend @Valdeirpsr, but now he saves the photo only without name..

  • Solved friend... fiucou thus $.post('library/save.php? name='+$("input[name=name]"). val() and managed to save with the name! thanks my friend!

No answers

Browser other questions tagged

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