wordpress 表单操作提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5137105/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:43:31  来源:igfitidea点击:

wordpress form action submit

wordpressforms

提问by Cam

have an own customized form in wordpress.

在 wordpress 中有自己的自定义表单。

action=<?php bloginfo('template_url'); ?>/send_form.php"

on submit, it goes always to send_form.php but this php is for sending the info - and i cant style with with the theme....

提交时,它总是发送到 send_form.php 但这个 php 用于发送信息 - 我不能用主题风格....

is there a way to stay on the current page while it sends the info and print out and message in a field that the form has been sended succesfully???

有没有办法在发送信息时停留在当前页面上并在表单已成功发送的字段中打印和消息???

anyone have some suggestion for me?

有人对我有什么建议吗?

回答by Greg

The answer to this is quite involved and getting it right takes a little care, but here are the essential points of one approach:

这个问题的答案非常复杂,正确处理需要一点小心,但这里是一种方法的要点:

  1. The action must be set to load the current page and you can do this by changing the actionattribute in the <form>tag to: action=""
  2. In the template file for this page, detect when the page loads after a form submission. You can do this by checking the state of the $_POST variable.
  3. Ifthere are post variables, process the form submission in the template file. You need to look at what happens in "send_form.php" to figure out what to do. Take care that you don't introduce security issues (e.g. be sure to use the NONCE).
  4. Redirect back to the same page to get rid of the post variables. If you don't do this people will get those "Do you want to resubmit the form" warning messages. See the PHP header() functionfor doing the redirect and note that this must happen before any output is sent to the page.
  5. Use server sessionsto display a "Form submission successful". Set a session variable to your message before the redirect, then detect it when the page loads, display it, and remove it so the message doesn't display the next time the page loads.
  1. 该操作必须设置为加载当前页面,您可以通过action<form>标记中的属性更改为:action=""
  2. 在此页面的模板文件中,检测表单提交后页面何时加载。您可以通过检查$_POST 变量的状态来做到这一点。
  3. 如果有post变量,处理模板文件中的表单提交。您需要查看“send_form.php”中发生的情况以找出要执行的操作。注意不要引入安全问题(例如,一定要使用 NONCE)。
  4. 重定向回同一页面以摆脱 post 变量。如果您不这样做,人们将收到“您想重新提交表单吗”警告消息。请参阅PHP header() 函数进行重定向,并注意这必须在任何输出发送到页面之前发生。
  5. 使用服务器会话显示“表单提交成功”。在重定向之前为您的消息设置一个会话变量,然后在页面加载时检测它,显示它并删除它,以便在下次加载页面时不会显示该消息。

That should give you a starting point.

这应该给你一个起点。

If anyone else has a simpler solution, I'd love to hear it too.

如果其他人有更简单的解决方案,我也很想听听。

回答by sudip

If you want to do it through plugin instead of the theme, then use "admin_post"(admin_post_YOUR_ACTION) and "admin_post_nopriv" (admin_post_nopriv_YOUR_ACTION) actions. (Btw, you can use those actions through theme also)

如果您想通过插件而不是主题来完成,请使用“admin_post”(admin_post_YOUR_ACTION) 和“admin_post_nopriv”(admin_post_nopriv_YOUR_ACTION) 操作。(顺便说一句,您也可以通过主题使用这些操作)

Here is a good explanation: https://www.sitepoint.com/handling-post-requests-the-wordpress-way/

这是一个很好的解释:https: //www.sitepoint.com/handling-post-requests-the-wordpress-way/