如何从外部js文件获取PHP Var? 但是,此变量是表中使用的动态变量。 重复此Access PHP var from external javascript file,但数据显示的结果相同
这是我的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('');
}
});
注意:我将在表格列表中的模式列表中显示
答案 0 :(得分:1)
有很多方法可以从外部/内部Java脚本访问PHP变量。
<?php $example_php_var = 'Example';?>
<html>
<head>
<title>Example</title>
<script>
(function(){ window.example = '<?php echo $example_php_var;?>'; })();
</script>
</head>
<body>
</body>
</html>
通过Ajax。示例---
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);
<?php $example_php_var = 'Example';?>
(function(){
var example = '<?php echo $example_php_var;?>';
alert(example);
})();
<?php
header('Content-type:text/javascript');
?>
您可以将上述php文件用作javascript链接-
<script src="example.php"></script>