简体   繁体   English

如何在javascript上获取PHP var?

[英]How to get PHP var on javascript?

How to get PHP Var from external js file?如何从外部js文件中获取PHP Var? 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 从外部 javascript 文件尝试这个Access PHP var,但数据显示相同的结果

this my js code这是我的 js 代码

 $('[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注意:我将在表格列表中的模态上显示此内容

There is many way to access PHP variable from external/internal java-script.有很多方法可以从外部/内部 java 脚本访问 PHP 变量。

  1. By declaring global js variable to header.通过将全局 js 变量声明为 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 ajax.php

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

example.js例子.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 文件作为 javascript example.php (输出为 javascript。你可以使用这个 php 文件链接作为 js 链接)
<?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 -您可以使用上面提到的 php 文件作为 javascript 链接 -

<script src="example.php"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM