我需要填写表格,转到下拉列表中选择的页面,然后使用表格中的其他信息填充所选页面
我之前问过两个问题 将信息放入表单中并使用它来填充页面,以及一个提交表单时根据您在表单下拉列表中选择的内容将您重定向到页面。现在我需要这两件事都适用于同一个表单。
从表单中获取信息并使用它来填充下一页的代码是:
<?php
$firstname = $_GET['personsName'];
echo "My Name is" .$firstname;
?>
表单看起来像这样:
<form action="letter.php" method="get">
<input type="text" name="personsName"></input>
<input type="submit" value="submit">
</form>
选择页面的代码是:
$pages = array('Page 1' => 'page1.php', 'Page 2' => 'page2.php', 'Page 3' => 'page3.php');
if (array_key_exists($_POST['dropdown-name'], $pages)) {
header("Location: " . $pages[$_POST['dropdown-name']]);
} else {
echo "Error processing form"; // submitted form value wasn't in your array, perhaps a hack attempt
}
我需要这两个都适用于同一个表单,我只是一直没能弄清楚。我尝试的是:
<form action="<?=$pages?>" method="POST">
<input type="text" name="name" /><br />
<select name="letter">
<option value="Page 1">
Page 1
</option>
<option value="Page 2">
Page 2
</option>
<option value="Page 3">
Page 3
</option>
</select>
<input type="submit" value"Print" />
</form>
这显然是错误的。我需要此表单来获取放入其中的信息,重定向到所选页面,然后使用输入到表单中的其他信息填充该页面。问题是我不知道如何得到我已经有了的答案。感谢所有的帮助!
当前代码是这样的:
<script type="text/javascript">
$(function UpdateFormAction(){
alert('Launched event handler');
var form = document.getElementById('MyForm');
var list = document.getElementById('PageList');
alert('List item numer ' + list.selectedIndex);
var desiredAction = list.options[list.selectedIndex].value
alert('Desired action set to ' + desiredAction);
form.action = desiredAction;
});
</script>
形式是:
<form id="MyForm" action="letter.php" method="POST">
<input type="text" name="name" /><br />
<select id="PageList" name="letter" onchange="UpdateFormAction();">
<option value="letter.php">Page 1</option>
<option value="letter2.php">Page 2</option>
<option value="letter3.php">Page 3</option>
</select>
<input type="submit" value"Print" />
</form>
I have asked two previous questions for taking info put into a form and using it to populate a page, and one to redirect you to a page when you submit the form based off of what you choose in the forms drop down. Now I need these two things to both work for the same form.
The code for taking the info from the form and using it to populae the next page is:
<?php
$firstname = $_GET['personsName'];
echo "My Name is" .$firstname;
?>
And the form would look like this:
<form action="letter.php" method="get">
<input type="text" name="personsName"></input>
<input type="submit" value="submit">
</form>
The code for selecting a page is:
$pages = array('Page 1' => 'page1.php', 'Page 2' => 'page2.php', 'Page 3' => 'page3.php');
if (array_key_exists($_POST['dropdown-name'], $pages)) {
header("Location: " . $pages[$_POST['dropdown-name']]);
} else {
echo "Error processing form"; // submitted form value wasn't in your array, perhaps a hack attempt
}
I need for both of these to work for the same form, I just haven't been able to figure it out. What I tried was:
<form action="<?=$pages?>" method="POST">
<input type="text" name="name" /><br />
<select name="letter">
<option value="Page 1">
Page 1
</option>
<option value="Page 2">
Page 2
</option>
<option value="Page 3">
Page 3
</option>
</select>
<input type="submit" value"Print" />
</form>
This is obviously wrong. I need this form to take the info put into it, redirect to the page chosen, then populate the page with the other info inputted into the form. The problem is I have no idea how to get the answers I already have together. Thank you to all that help!
Current code is this:
<script type="text/javascript">
$(function UpdateFormAction(){
alert('Launched event handler');
var form = document.getElementById('MyForm');
var list = document.getElementById('PageList');
alert('List item numer ' + list.selectedIndex);
var desiredAction = list.options[list.selectedIndex].value
alert('Desired action set to ' + desiredAction);
form.action = desiredAction;
});
</script>
Form is:
<form id="MyForm" action="letter.php" method="POST">
<input type="text" name="name" /><br />
<select id="PageList" name="letter" onchange="UpdateFormAction();">
<option value="letter.php">Page 1</option>
<option value="letter2.php">Page 2</option>
<option value="letter3.php">Page 3</option>
</select>
<input type="submit" value"Print" />
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行重定向时,您将丢失发送到服务器的数据。
基本上,您有 3 个选项:
所以类似:
这似乎是您的简单示例的最佳答案。如果有大量数据或更复杂,请考虑使用上面的其他 2 个选项之一
注意:在您调用变量
name
的表单上,在接收它的 pae 上,您调用itpersonsName
- 注意 URL 中从一个到另一个的映射。JavaScript 解决方案(已测试):
You're losing the data sent to the server when you do the redirect.
Basically, you've got 3 options:
So something like:
Which seems to be the best answer for your simple example. If there's a LOT of data or it's more complex, consider using one of the other 2 option above
NB: On the form you've called the variable
name
, on the pae receiving it, you've called itpersonsName
- note the mapping from one to the other in the URL.Javascript solution (tested):