如何将表单发布到WordPress的另一个PHP文件?

编程入门 行业动态 更新时间:2024-10-27 08:28:18
本文介绍了如何将表单发布到WordPress的另一个PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我的应用程序主题结构是 -wp-content -主题 -cool -templates -landing.php - post-landing.php

我在landing.php中有一个表单,我想在post-landing.php中处理POST请求,我有这个请求,

form action =wp-content / themes / cool / templates / post-landing.php

但是当我发布一个post请求时,它说 404。

解决方案

还有一种方法:如果你想异步管理它,你可以像这样创建一个AJAX调用(我通常将它放在我的< theme> / inc 文件夹中,所以它的自动加载):

ADD_ACTION( 'wp_ajax_nopriv_< call_name>', '<作用名称>'); add_action('wp_ajax_< call_name>','<函数名称>'); function< function_name>(){//这里你用$ _POST做了一些事情并返回json}

在您的 functions.php 文件中进行本地化,以便让请求的ajaxUrl始终可用于变量并仅用于您的js文件:

wp_localize_script( '排队-JS-名', '的varName',阵列( 'ajaxUrl'=> admin_url('admin-ajax.php')));

然后你可以在入队的JS文件中调用它:

'pre> jQuery.ajax({网址:varName.ajaxUrl,型: '后',数据:currentDataToSubmit, 成功:函数(响应){ //管理响应} });

就是这样!希望它有用!

如果第一部分出现问题,请告诉我,我也可以学习!

So my app theme structure is

-wp-content -themes -cool -templates -landing.php - post-landing.php

I have a form in landing.php and I want to handle POST request in post-landing.php for which I have,

form action="wp-content/themes/cool/templates/post-landing.php"

But when I make a post request it says 404.

解决方案

Here's another way: in case you want to manage that asynchronously, you can create an AJAX call like this(I usually place it in my <theme>/inc folder, so it's automatically loaded):

add_action( 'wp_ajax_nopriv_<call_name>', '<function name>' ); add_action( 'wp_ajax_<call_name>', '<function name>' ); function <function_name>() {//here you do things with $_POST and return json}

Localize it in your functions.php file in order to have your ajaxUrl for the request always available in a variable and only for your js file:

wp_localize_script( 'enqueued-js-name', 'varName', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ) ));

And then you can call it like this in your enqueued JS file:

jQuery.ajax({ url: varName.ajaxUrl, type: 'post', data: currentDataToSubmit, success: function(response){ //manage the response } });

And that's it! hope it was useful!

If there's something wrong with the first part, let me know, so I can learn too!

更多推荐

如何将表单发布到WordPress的另一个PHP文件?

本文发布于:2023-07-25 12:12:41,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   如何将   文件   WordPress   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!