无需使用PHP进行重定向的POST [英] POST without redirect with PHP

查看:59
本文介绍了无需使用PHP进行重定向的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的邮件列表表单,可以在 http://www中找到. notonebit.com/projects/mailing-list/

I have a simple form for a mailing list that I found at 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>     

推荐答案

您将需要AJAX将数据发布到您的服务器.最好的解决方案是实施常规过帐,这样至少可以正常工作.然后,您可以使用Javascript进行连接.这样,当某人没有Javascript时,发布(刷新)将起作用.

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.

如果找到 a关于使用JQuery使用AJAX发布表单的好文章.

此外,您可以选择将数据发布到相同的URL. JQuery库将添加HTTP_X_REQUESTED_WITH标头,您可以在服务器端脚本中检查其中的值.这将使您可以发布到相同的URL,但返回不同的值(整个页面,或者只是一个特定的响应,具体取决于是否为AJAX请求). 因此,您实际上可以从表单中获取该url,并且也不需要在Javascript中对其进行编码.这样一来,您就可以编写一个更具可维护性的脚本,甚至可能会导致一种通用的表单处理方法,您可以将其重用于要使用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.

这篇关于无需使用PHP进行重定向的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆