我有一个可以触发贝宝或其他链接的表单。
如何通过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
}
使用:
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
}
只需使用header
:
header('Location: http://www.example.com/');
在php中,可以使用header()
方法:
if($_POST["action1"]) {
header('Location: https://www.paypal.com/cgi-bin/webscr');
}
if($_POST["action2"]) {
header('Location: https://www.someotherwebsite.com/pay');
}
使用隐藏输入。
<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');
}
只需检查是否存在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
}