Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
How to pass JavaScript variable into a PHP function parameter?

I am trying to get a response from a Twilio API where I am facing complexity. I have a javascript variable called sid which is containing a string value.

var sid = value['sid']; // 'CA519c6aefde211131f2f44370d67607d4'

Now I need to call this variable into a PHP function as a parameter.

<?php echo $twilio->check_disputed($parameter) ?>

So, the $parameter is coming from the Javascript var sid.

I want to place the javascript variable as the parameter.

Thanks <3

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In a very simple form/example you simply need to send an http request ( here done using the Fetch api ) to your PHP script and intercept that request. The following sends a POST request with a single parameter sid which is then passed to the Twilio method as per your requirement.

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['sid'] ) ){
        $sid=$_POST['sid'];
        exit( $twilio->check_disputed( $sid ) );
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>Your page</title>
    </head>
    <body>
        <h1>Do lots of things...</h1>
        
        <script>
            /*
                do whatever it is you do to create your variable sid
                - here it is explicitly declared rather than derived.
            */
            const sid='CA519c6aefde211131f2f44370d67607d4';
            
            let fd=new FormData();
                fd.set('sid',sid);
                
            fetch( location.href, { method:'post', body:fd } )
                .then(r=>r.text())
                .then(data=>{
                    alert(data)
                })
        </script>
    </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Asynchronious request to achive that. first import your jquery library

  var actual_url = "yourDomain/data=" + data_you_want_to_pass;
  $.ajax({
            url: actual_url,
            type: 'GET',
            dataType: 'text', // added data type
            success: function(res) {
                //log if you have any response
                console.log(res);
            }
        });

now you can recieve the data on your php file using GET method.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda