0

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

Pri Nce
  • 576
  • 6
  • 18
  • 2
    you need to send an HTTP request ( ajax most likely ) to your PHP script ( not shown!! ) and process that request with PHP. – Professor Abronsius Jun 18 '22 at 06:58
  • Yes, because your JavaScript is running on the _client_ and PHP on the _server_. – ndc85430 Jun 18 '22 at 07:05
  • if for any reason you don't want to post anything you can use this example: (file=index.php ) yooopy –  Jun 18 '22 at 08:56

2 Answers2

2

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>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
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.

gsharew
  • 263
  • 1
  • 12