![](/img/trans.png)
[英]I'm trying to link my form to my PHP page, and I want to print out the user's input onto that page when he clicks the submit button
[英]In PHP how do I send form data to a page but direct the user to another when he clicks the submit button?
以这个简单的形式为例:
<form method="post" action="reports.php">
<input type="text" name="name">
<input type="text" name="email_address">
<input type="submit" value="Submit">
我希望它将输入字段数据发送到reports.php
页面,但是当他单击"Submit"
按钮时,我希望它将用户定向到页面uploads.php
。
不能在reports.php
页面上使用header("Location: uploads.php")
,因为reports.php
会将表单数据收集到一个表格中,以供所有人查看。 在uploads.php
页面中,用户将上传在reports.php
表中也可见的文件。
我该怎么做呢?
您可以检查对reports.php
请求中使用的方法是否为POST
,然后重定向用户。
<?php
# reports.php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
// process the data
header("Location: ...");
exit;
}
或者,您可以简单地创建一个add_report.php
文件。
您可以使用javascript添加“表单提交”事件。
单击“提交”按钮后-使用javascript收集表单数据并将其发送到需要的地方。 当ajax工作时-使用javascript将用户重定向到所需的页面。
完整的示例如下:
对于html
<form method="post" id="my_form">
<input type="text" name="name">
<input type="text" name="email_address">
<input type="submit" value="Submit">
对于javascript
<script>
$(function(){
$('#my_form').on('submit', function(e){
e.preventDefault();
var url = 'reports.php?' + $(this).serialize();
$.get(url, function(){
window.location.href = 'uploads.php';
});
})
})
</script>
更多信息可以在这里找到: https : //api.jquery.com/serialize/
您需要重构代码。
听起来您的用例是:
不必担心使“某处”成为报告文件。
将“添加到报告”代码分离到一个(或多个)函数中,并将其放入自己的文件中。
然后在报告和上传文件中都包含该文件。
使上传表单提交上传,然后调用“添加到报告”功能。
在报告中执行类似的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.