2015-02-07 40 views
1

我有一個可以觸發paypal或其他鏈接的表單。如何通過PHP觸發表單動作?

如何通過PHP觸發這些操作?

HTML:

<form action="myphp.php" method="POST /> 
    <input type="submit" value="paypal" name="action1" /> 
    <input type="submit" value="other" name="action2"> 
</form> 

myphp.php:

if($_POST["action1"]) { 
//https://www.paypal.com/cgi-bin/webscr should trigger here 
} 
if($_POST["action2"]) { 
//https://www.someotherwebsite.com/pay should trigger here 
} 
+1

這個問題不是很清楚,你是什麼意思 「扳機」 是什麼意思? – MightyPork 2015-02-07 14:39:34

回答

0

用途:

if($_POST["action1"]) { 
header('Location: https://www.paypal.com/cgi-bin/webscr'); 
exit;//it's a good habit to call exit after header function because script don't stop executing after redirection 
} 
+0

謝謝,它工作。 – user3757605 2015-02-07 15:08:53

+0

不客氣!我很高興它有幫助。 – Whirlwind 2015-02-07 15:09:55

0

只需使用header

header('Location: http://www.example.com/'); 
0

在PHP中,你可以做到這一點與header()方法:

if($_POST["action1"]) { 
    header('Location: https://www.paypal.com/cgi-bin/webscr'); 
} 
if($_POST["action2"]) { 
    header('Location: https://www.someotherwebsite.com/pay'); 
} 
0

使用一個隱藏的輸入。

<form action="myphp.php" name="form1" method="POST" /> 
    <input type="hidden" name="action" /> 
    <input onclick="setHidden(this)" type="button" value="paypal" /> 
    <input onclick="setHidden(this)" type="button" value="other" /> 
</form> 

<script> 
    function setHidden(element) { 
     document.form1.action.value = element.value; 
     document.form1.submit(); 
    } 
</script> 

然後在PHP

if($_POST["action"] == "paypal") { 
    header('Location: https://www.paypal.com/cgi-bin/webscr'); 
} 
else if($_POST["action"] == "other") { 
    header('Location: https://www.someotherwebsite.com/pay'); 
} 
0

只是檢查是否存在isset

if(isset($_POST["action1"])) { 
//https://www.paypal.com/cgi-bin/webscr should trigger here 
} 
if(isset($_POST["action2"])) { 
//https://www.someotherwebsite.com/pay should trigger here 
}