Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to run this javascript below but am not seeing the output of the php shell_exec command.

Running the test -a bash script will output a series of ID's 34535, 25643, 23262, and so on. When I run it in my php file with a simple it works fine.

print shell_exec('/opt/bin/echkchunk -a');

But when I try to run it below and I select test1 there is nothing outputted to the screen. Looking through chromes developer tools I see the code when test1 is selected as the following

<!--?php shell_exec("/opt/bin/echkchunk -a"); ?-->

as if it is commented out.

So my question is, is it possible to run the bash script this way with php and JavaScript? Or is there another way to get that information displayed to the webpage without 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>
share|improve this question
2  
The way you want to receive the date is to do an ajax request to a php file that does the reading for you. –  Dainis Abols Oct 16 '12 at 8:10
1  
That would be much cleaner than embedding the PHP in the JavaScript. –  jmort253 Oct 16 '12 at 8:15
    
Thanks Dainis, I separated out the php with some ajax and now the script is working correctly. –  user1749264 Oct 16 '12 at 11:02

2 Answers 2

If your result is multi line, you need to replace the newline marker with escape sequence or HTML. Try to replace

var tester = '<?php shell_exec("/opt/bin/test -a"); ?>';

with

var tester = '<?php echo str_replace(PHP_EOL, '<br/>', shell_exec("/opt/bin/test -a")); ?>';
share|improve this answer
    
Thanks everyone for all the help, Dainis suggestion of separating out the php and using some ajax was what worked for me. –  user1749264 Oct 16 '12 at 11:05

Try running your command copying the output to the stdout:

<?php shell_exec("/opt/bin/echkchunk -a 2>&1"); ?>

shell_exec returns null if an error occurs running the command, using 2>&1 your are getting the whole output even if the command fails.

See sh command: exec 2>&1

share|improve this answer
    
I edited your post to format the code properly, but you should consider explaining what this does exactly. I'm sure an explanation would help the original poster as well as all the future visitors. Good luck! :) –  jmort253 Oct 16 '12 at 8:16
    
Thanks for the advice! And I should add that I used that code when trying to get the output of convert for resizing images. And @ user1749264! you should try echoing the output ;) –  Saul Martínez Oct 16 '12 at 8:22
    
Cool, It's awesome running bash from PHP ;) I know you've been a member of SO for awhile, but as a refresher, you can use this edit link to add the explanation to your answer. Hope this helps! :) –  jmort253 Oct 16 '12 at 8:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.