take the result of one function and send via post in another

Asked

Viewed 109 times

0

I am using the lib instascan to read qr code. but I am unable to send the result via post with ajax. anyone can help?

The code I’m using is below:

qr

<h1>qr scanner</h1>

<video id="preview"></video>
<div id="conteudo"></div>
<script type="text/javascript">
  let scanner = new Instascan.Scanner({ video: document.getElementById('preview') });
  scanner.addListener('scan', function lerQrcode(content) {
    //alert(content);
    gravar(content);
    document.getElementById('conteudo').innerHTML = content;

  });

  function gravar(){        
        $.ajax({
            method: "POST",
            url: "dados.php",
            data: '', // aqui deveria executar o post dos dados lidos
            success: function(data){                                
                alert(data.teste);
            }
        });
}


  Instascan.Camera.getCameras().then(function (cameras) {
    if (cameras.length > 0) {
      scanner.start(cameras[0]);
    } else {
      console.error('No cameras found.');
    }
  }).catch(function (e) {
    console.error(e);
  });
  // executar o post


</script>

  • The date is '', it should be content, right? To do this you would create a global variable to receive the content value in the scanner scanner scanner and pass in the date attribute this variable.

1 answer

0

You are calling the function gravar(content) sending the value of content, then you must put in the function the respective parameter to receive this value, and play in the date of Ajax:

function gravar(content){
   $.ajax({
      method: "POST",
      url: "dados.php",
      data: { codigo: content },
      success: function(data){                                
         alert(data.teste);
      }
   });
}

On the PHP page (dados.php) you receive the value with $_POST['codigo'].

Browser other questions tagged

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