2013-04-14 50 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