Php 提交表单时重定向用户
嘿,我想使用php将用户重定向到url,这取决于他们单击submit按钮后在表单中选择的单选按钮 到目前为止,我有这个,但它不工作,所以它是非常无用的Php 提交表单时重定向用户,php,html,forms,Php,Html,Forms,嘿,我想使用php将用户重定向到url,这取决于他们单击submit按钮后在表单中选择的单选按钮 到目前为止,我有这个,但它不工作,所以它是非常无用的 <?php // variables form form $Country = $_POST['Country']; ?> <form action="<?php print $Country ?>" method="post">
<?php // variables form form
$Country = $_POST['Country'];
?>
<form action="<?php print $Country ?>" method="post">
<input name="Country" type="radio" value="http://www.istockphoto.com" /> South Africa<br />
<input name="Country" type="radio" value="http://www.jamieburger.co.za" /> England<br />
<input name="" type="submit" value="Submit"/>
</form>
使用位置标头重定向:
if ($Country == 'England') {
header('Location: http://www.jamieburger.co.za');
}
如果您想在提交时重定向,您需要使用JavaScript根据您的“无线电”状态更改提交URL。我建议您让服务器决定将用户重定向到哪个URL
只需在服务器端执行以下操作:
header("Location: ".$Country);
exit();
但是(!!)检查URL是否有效,否则您将重定向到用户提交的任何URL。将国家名称作为单选按钮的“值”也是更好的样式。例如
<input type="radio" name="Country" value="Germany">Germany</input>
使用header()
示例:
<?php
if (isset($_POST['Country'])) header('Location: $_POST['Country']');
?>
请记住将此代码放在页面的最顶端(在任何HTML标记之前)
首先,表单的action参数应该是一个url,可以是绝对的,也可以是相对的,尽管通常是相对的
<form action='process.php?action=country'>
// submit to the same calling page
<form action='?action=country'>
注意:最好在数组中编码URL,并使用单选按钮的索引获取要重定向到的正确URL。它还使从数据库中读取要重定向到的URL变得更容易。303重定向就是专门为此而设计的。简单地标题('Location:http://')
实际上发送了一个302,这在HTTP/1.1中有点不推荐使用。我是这样做的:
<?php // variables form form
if (!empty($_POST['Country']) {
header('location: ' . $_POST['Country']);
}
?>
<form action="" method="post">
<input name="Country" type="radio" value="http://www.istockphoto.com" /> South Africa<br />
<input name="Country" type="radio" value="http://www.jamieburger.co.za" /> England<br />
<input name="" type="submit" value="Submit"/>
</form>
header($_SERVER['SERVER_PROTOCOL'] . ' 303 See Other', true, 303);
header('Location: ' . $url);
您可能需要使用标题('Location:'。$Country)
、一个if(isset)
和一个PHP\u SELF
记住使用带有位置的绝对URL。相对路径不能保证工作。他想在客户端重定向。谢谢大家的评论。现在效果很好,我只需要让它在我的facebook应用程序中工作。
<?php
$country = $_POST['Country'];
// redirect
header("Location: $Country");
exit();
?>
<?php // variables form form
if (!empty($_POST['Country']) {
header('location: ' . $_POST['Country']);
}
?>
<form action="" method="post">
<input name="Country" type="radio" value="http://www.istockphoto.com" /> South Africa<br />
<input name="Country" type="radio" value="http://www.jamieburger.co.za" /> England<br />
<input name="" type="submit" value="Submit"/>
</form>
header($_SERVER['SERVER_PROTOCOL'] . ' 303 See Other', true, 303);
header('Location: ' . $url);