【发布时间】:2012-10-16 08:05:55
【问题描述】:
我正在尝试在下面运行这个 javascript,但没有看到 php shell_exec 命令的输出。
运行 test -a bash 脚本会输出一系列 ID 的 34535、25643、23262 等。当我在我的 php 文件中运行它时,它运行良好。
print shell_exec('/opt/bin/echkchunk -a');
但是当我尝试在下面运行它并选择 test1 时,屏幕上没有任何输出。浏览 chromes 开发者工具时,我看到 test1 被选中时的代码如下
<!--?php shell_exec("/opt/bin/echkchunk -a"); ?-->
好像它被注释掉了。
所以我的问题是,是否可以使用 php 和 JavaScript 以这种方式运行 bash 脚本?或者有没有其他方法可以在没有 JavaScript 的情况下将该信息显示到网页上?
<script type="text/javascript">
var tester = '<?php shell_exec("/opt/bin/test -a"); ?>';
$(document).ready(function() {
$("#selector").on("change", function() {
if ($("#selector").val() == "test1") {
$("#rightselection").css("background-color", "red");
$("#rightselection").html(tester);
}
if ($("#selector").val() == "test2") {
$("#rightselection").css("background-color", "blue");
$("#rightselection").html("test2");
}
if ($("#selector").val() == "test3"){
$("#rightselection").css("background-color", "yellow");
$("#rightselection").html("test3");
}
if ($("#selector").val() == ""){
$("#rightselection").css("background-color", "green");
$("#rightselection").html("This is a Test");
}
});
});
</script>
【问题讨论】:
-
您希望接收日期的方式是向为您读取的 php 文件发出 ajax 请求。
-
这比将 PHP 嵌入到 JavaScript 中要干净得多。
-
感谢 Dainis,我用一些 ajax 分离了 php,现在脚本可以正常工作了。
标签: php javascript html bash