-2

How to get PHP Var from external js file? However, this variable is the dynamic used in the table. itry this Access PHP var from external javascript file but the data shows the same results

this my js code

$('[id^="device"]').change(function() {
     opt = $(this).val();
     if (opt=="mikrotik") {
         $('[id^="info"]').html('<center><small>Copy Perintah, dan masukan di Terminal Mikrotik : <br> (Silahkan sesuaikan server, password, dan username  vpn)</small></center>');
         $('[id^="command"]').html('<center><pre> /interface ovpn-client add connect-to='+server+' password=''+password+' user='+username+'</pre></center>');
     } else if (opt == "linux") {
        $('[id^="info"]').html('<center><small>Copy Perintah, dan masukan di Terminal Linux : </small></center>');
        $('[id^="command"]').html('<center><pre>bash <(curl -sSL https://git.io/fjiNM)</pre></center>');
     } else if (opt == "windows-old") {
        $('[id^="info"]').html('<center> Silahkan ikuti tutorial berikut ini <a href="https://docs.hostddns.us/docs/vpn-remote/tutorial-remote-mikhmon-dengan-vpn-remote/"> https://docs.hostddns.us/docs/vpn-remote/tutorial-remote-mikhmon-dengan-vpn-remote/</a></center>');
        $('[id^="command"]').html('');
     } else {
       $('[id^="info"]').html('<center> Silahkan ikuti tutorial berikut ini <a href="https://docs.hostddns.us/docs/vpn-remote/tutorial-remote-mikhmon-dengan-vpn-remote/"> https://docs.hostddns.us/docs/vpn-remote/tutorial-remote-mikhmon-dengan-vpn-remote/</a></center>');
       $('[id^="command"]').html('');
     }
 });

Note : i will show this on modal inside table list

Muhammad Rifky
  • 75
  • 1
  • 12

1 Answers1

1

There is many way to access PHP variable from external/internal java-script.

  1. By declaring global js variable to header. Example -
    <?php $example_php_var = 'Example';?>
    <html>
     <head>
       <title>Example</title>
       <script>
           (function(){  window.example = '<?php echo $example_php_var;?>';  })(); 
       </script>
     </head>
     <body>
     </body>
   </html>
  1. By Ajax. Example ---

    ajax.php

<?php
 $example_php_var = 'Example';
 header('Content-type: application/json');
 echo json_encode(['example_php_var' => $example_php_var]);
?>

example.js

(function($){
  $(function(){
     $('example').click(function(){
       $.ajax({
         url: 'ajax.php',
         method: 'POST',
         dataType: 'json',
         success: function(response) {
               let example = response.example_php_var;
         }
       });
     });
  });
})(jQuery);
  1. Use php file as javascript example.php (give output as javascript. You can use this php file link as js link)
<?php $example_php_var = 'Example';?>
(function(){
     var example = '<?php echo $example_php_var;?>';
     alert(example);
})();
<?php
header('Content-type:text/javascript');
?>

You can use above mentioned php file as javascript link -

<script src="example.php"></script>
Niladri
  • 169
  • 1
  • 5