2017-08-07 75 views
1

我想在我的服務器上從單選按鈕執行一個bash腳本並在我的網頁上提交按鈕。有兩個單選按鈕,每個都會執行不同的bash腳本(test1.sh test2.sh)。這是我到目前爲止。使用php執行bash腳本的HTML表單單選按鈕。

網頁HTML:

<form action="testexec.php" method="get"> 
<label class="col">Up/Down</label> 
<span class="col"> 
    <input type="radio" name="option" id="r1" value="1" /> 
    <label for="r1">Up</label> 
    <input type="radio" name="option" id="r2" value="2" /> 
    <label for="r2">Down</label> 
</span> 
<span class="col"> 
    <input type="submit" /> 
</span> 

test1.sh/test2.sh(在/ var/www/html等/ testscripts)

#!/bin/bash 
touch /tmp/testfile 
ls -ltr /tmp 

testexec.php(同一目錄中我網頁)

<?php 
$output = shell_exec("/var/www/html/testscript/test.sh"); 
header('Location: http://psat/moslehpour/dadt/main/systemTest.php? 
success=true'); 
echo "<pre>$output</pre>" 
?> 

當點擊提交按鈕時,我在服務器端看不到任何操作。另外,我如何驗證正在發生的事情,我試圖回顯到網頁,但似乎沒有顯示。

+0

如果你想看到你的輸出,你需要刪除重定向 –

+0

你也叫你輸入字段都「選項」,這將只是發送選項= 2。你必須將其更改爲選項[],並在你的PHP腳本$ _GET [選項] [0],$ _GET [選項] [1]獲得的值或只是他們的名字diefferently –

+0

@MarouenMhiri當我設置的選項選項1和選項2,它將允許我選擇兩個單選按鈕。我如何設置選項以僅允許用戶選擇一個選項? – kkmoslehpour

回答

1

嘗試這樣的事情。

<form action="testexec.php" method="POST"> 
    <label class="col">Up/Down</label> 
    <span class="col"> 
    <input type="radio" name="option" id="r1" >test1 
    <label for="r1">Up</label> 
    <input type="radio" name="option" id="r2" >test2 
    <label for="r2">Down</label> 
</span> 
<span class="col"> 
<input type="submit" /> 

<?php 
//when the submit button is clicked 
if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 

//name the radio 1 option1 
if(isset($_POST['option']) && $_POST['option'] == 'test1') 
{ $output = shell_exec("/var/www/html/testscript/test.sh");} 

//name the radio 2 option1 
if(isset($_POST['option']) && $_POST['option'] == 'test2') 
{ $output = shell_exec("/var/www/html/testscript/test.sh");} 

//remove this if you want to see $output in this file 
header('Location: http://psat/moslehpour/dadt/main/systemTest.php? 
success=true'); 

echo "<pre>$output</pre>" 
} 
?> 
+0

我是否必須將方法更改爲在我的html頁面中進行POST? – kkmoslehpour

+0

另外,爲什麼你更喜歡郵寄GET? – kkmoslehpour

+0

@kkmoslehpour 1)修改後的方法... –