POST 不使用 PHP 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6806028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST without redirect with PHP
提问by dasaki
I have a simple form for a mailing list that I found at http://www.notonebit.com/projects/mailing-list/
我在http://www.notonebit.com/projects/mailing-list/找到了一个简单的邮件列表表格
The problem is when I click submit all I want it to do is display a message under the current form saying "Thanks for subscribing" without any redirect. Instead, it directs me to a completely new page.
问题是当我点击提交时,我想要它做的就是在当前表单下显示一条消息,说“感谢订阅”而没有任何重定向。相反,它会将我引导到一个全新的页面。
<form method="POST" action="mlml/process.php">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="submit" value="" id="submit"name="submit" >
</form>
采纳答案by GolezTrol
You will need AJAX to post the data to your server. The best solution is to implement the regular posting, so that will at least work. Then, you can hook into that using Javascript. That way, posting will work (with a refresh) when someone doesn't have Javascript.
您将需要 AJAX 将数据发布到您的服务器。最好的解决方案是实施定期发布,这样至少会起作用。然后,您可以使用 Javascript 挂钩。这样,当有人没有 Javascript 时,发布将起作用(刷新)。
If found a good article on posting forms with AJAX using JQuery.
如果找到一篇关于使用 JQuery 使用 AJAX 发布表单的好文章。
In addition, you can choose to post the data to the same url. The JQuery library will add the HTTP_X_REQUESTED_WITH
header, of which you can check the value in your server side script. That will allow you to post to the same url but return a different value (entire page, or just a specific response, depending on being an AJAX request or not).
So you can actually get the url from your form and won't need to code it in your Javascript too. That allows you to write a more maintanable script, and may even lead to a generic form handling method that you can reuse for all forms you want to post using Ajax.
此外,您可以选择将数据发布到相同的 url。JQuery 库将添加HTTP_X_REQUESTED_WITH
标头,您可以在服务器端脚本中检查其值。这将允许您发布到相同的 url 但返回不同的值(整个页面,或者只是一个特定的响应,取决于是否是 AJAX 请求)。因此,您实际上可以从表单中获取 url,而无需在 Javascript 中对其进行编码。这允许您编写一个更易于维护的脚本,甚至可能导致您可以对所有要使用 Ajax 发布的表单重用的通用表单处理方法。
回答by Quasdunk
Quite simple with jQuery:
使用 jQuery 非常简单:
<form id="mail_subscribe">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="hidden" name="action" value="subscribe" />
<input type="submit" value="" id="submit"name="submit" >
</form>
<p style="display: none;" id="notification">Thank You!</p>
<script>
$('#mail_subscribe').submit(function() {
var post_data = $('#mail_subscribe').serialize();
$.post('mlml/process.php', post_data, function(data) {
$('#notification').show();
});
});
</script>
and in your process.php:
在你的 process.php 中:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'subscribe' :
$email_address = $_POST['address'];
//do some db stuff...
//if you echo out something, it will be available in the data-argument of the
//ajax-post-callback-function and can be displayed on the html-site
break;
}
}
?>
回答by Josh
It redirects to a different page because of your action
attribute.
由于您的action
属性,它重定向到不同的页面。
Try:
尝试:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="address" id="email" maxlength="30" size="23" />
<input type="submit" value="" id="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) : ?>
<p>Thank you for subscribing!</p>
<?php endif; ?>
The page will show your "Thank You" message after the user clicks your submit button.
用户点击您的提交按钮后,页面将显示您的“谢谢”消息。
Also, since I don't know the name of the page your code is on, I inserted a superglobal variable that will insert the the filename of the currently executing script, relative to the document root. So, this page will submit to itself.
此外,由于我不知道您的代码所在页面的名称,因此我插入了一个超全局变量,该变量将插入当前正在执行的脚本的文件名,相对于文档 root。所以,这个页面将提交给自己。
回答by GameScripting
You have to use AJAX. But that requires JavaScript to be active at the users Brwoser. In my opinion it's the only way to do without redirect.
你必须使用 AJAX。但这需要 JavaScript 在用户 Brwoser 上处于活动状态。在我看来,这是没有重定向的唯一方法。
回答by JustPassingBy
to send a form request without redirecting is impossible in php but there is a way you can work around it.
在 php 中发送表单请求而不重定向是不可能的,但有一种方法可以解决它。
<form method="post" action="http://yoururl.com/recv.php" target="_self">
<input type="text" name="somedata" id="somedata" />
<input type="submit" name="submit" value="Submit!" />
</form>
<form method="post" action="http://yoururl.com/recv.php" target="_self">
<input type="text" name="somedata" id="somedata" />
<input type="submit" name="submit" value="Submit!" />
</form>
then for the php page its sending to have it do something but DO NOT echo back a result, instead simply redirect using
然后对于 php 页面,它发送让它做一些事情,但不要回显结果,而只是使用重定向
header( 'Location: http://yourotherurl.com/formpage' );
header( 'Location: http://yourotherurl.com/formpage' );
if you want it to send back a success message simply do
如果您希望它发回成功消息,只需执行
$success = "true";
header( 'Location: http://yourotherurl.com/formpage?success='.$success);
$success = "true";
header( 'Location: http://yourotherurl.com/formpage?success='.$success);
and on the formpage add
并在表单页面上添加
$success = $_GET['success'];
if($success == "true"){ echo 'Your success message'; } else { echo 'Your failure message';
$success = $_GET['success'];
if($success == "true"){ echo '你的成功信息'; } else { echo '你的失败信息';
回答by Igarza
ENGLISH Version
英文版
It seems that no one has solved this problem without javascript or ajax
似乎没有人在没有 javascript 或 ajax 的情况下解决了这个问题
You can also do the following.
您还可以执行以下操作。
Save a php file with the functions and then send them to the index of your page
使用函数保存一个 php 文件,然后将它们发送到您的页面索引
Example
例子
INDEX.PHP
索引文件
<div>
<?php include 'tools/edit.php';?>
<form method="post">
<input type="submit" name="disable" value="Disable" />
<input type="submit" name="enable" value="Enable" />
</form>
</div>
Tools.php (It can be any name, note that it is kept in a folder lame tools)
Tools.php(可以是任意名称,注意保存在lame tools文件夹中)
<?php
if(isset($_POST['enable'])) {
echo "Enable";
} else {
}
if(isset($_POST['enable'])) {
echo "Enable";
} else {
}
?>
回答by Derick Schoonbee
Just an idea that might work for you assuming you have no control over the page you are posting to:
假设您无法控制要发布到的页面,这只是一个可能对您有用的想法:
Create your own "proxy php target" for action and then reply with the message you want. The data that was posted to your php file can then be forwarded with http_post_data(Perform POST request with pre-encoded data). You might need to parse it a bit.
创建您自己的“代理 php 目标”以进行操作,然后回复您想要的消息。然后可以使用http_post_data(使用预编码数据执行 POST 请求)转发发布到您的 php 文件的数据。您可能需要稍微解析一下。
回答by Prashant Singh
Use
用
form onsubmit="takeActions();return false;"
function takeAction(){ var value1 = document.getElementById('name').innerHTML; // make an AJAX call and send all the values to it // Once , you are done with AJAX, time to say Thanks :) document.getElementById('reqDiv').innerHTML = "Thank You for subscribing"; }