2013-04-14 49 views
1

我目前正在嘗試使超鏈接調用我所在的同一頁面,但使用不同的PHP函數。這似乎是一個足夠簡單的解決方案,但我不希望URL中顯示任何信息。 「帖子」似乎是最好的解決方案,但我無法找到任何有關如何完成這項工作的結果。如何使用超鏈接「發佈」樣式調用php函數

<?php 
function myFirst(){ 
    echo 'The First ran successfully.'; 
} 
function mySecond(){ 
    echo 'The Second ran successfully.'; 
} 
?> 

<html><body> 

<?php 

if (isset($_GET['run'])){ 
    $linkchoice=$_GET['run']; 
}else{ 
    $linkchoice=''; 
} 

switch($linkchoice){ 
    case 'first' : 
     myFirst(); 
     break; 

    case 'second' : 
     mySecond(); 
     break; 

    default : 
     echo 'no run'; 
} 

?> 

<hr> 
<a href="?run=first">Link to First</a> 
<br/> 
<a href="?run=second">Link to Second</a> 
<br/> 
<a href="?run=0">Refresh No run</a> 

</body></html> 
+0

什麼問題?怎麼了? – SLaks

+0

上面的代碼工作正常。然而;它會在URL中顯示函數「?run = first」。我想要一種方式,讓這不會發生 – theB3RV

+0

按鈕不是一個選項 – theB3RV

回答

4

如果你想通過按下頁面中的某些東西來使用POST,你可以使用JS將它變成POST請求,或者直接提交一個表單。

假設u使用jQuery的

<a href="baba/was/here/this/is/the/url" onclick="manda_mi()">go somewhere</a> 
<form id="koko" style="display:none" target="baba/was/here/this/is/the/url" method="post"> 
<input type="hidden" name="run" value="boo" /> 
</form> 
... 
... 
function manda_mi(){ 
    $('#koko').submit(); 
} 
+0

AJAX救援! – cereallarceny

+0

從他想要syncronus的問題來看......他可以用JS提交一個隱藏的表單,不需要AJAX。 –

+0

我可以得到一個js函數的例子來執行上面的代碼。 – theB3RV

0

如果你想避免使用Javascript,你可以換一個形式的聯繫,和風格的按鈕看起來像正常的鏈接。

基本上是:

<button type="submit" value="first" name="action">Link to First</button> 
<br/> 
<button type="submit" value="second" name="action">Link to Second</button> 
<br/> 
<button type="submit" value="0" name="run">Refresh no Run</button> 

然後就是檢查按下哪個按鈕。

雖然最簡單的選擇可能是Javascript。

+0

按鈕不是一個選項。 – theB3RV