繁体   English   中英

将表单字段传递给ajax,然后通过php发布

[英]Passing a form field to ajax, and then posting it via php

我试图修改Wordpress插件。 它使用前端表单,使用ajax / jquery处理后将提交内容发布到各种日志中。 我需要添加一个附加字段,将其值传递到.js文件,并发布附加字段。

到目前为止,我已经通过连接到现有函数中添加了附加字段。 该字段看起来像这样,并且在前端可见。

$description_input = '<input type="text" id="custom_message" 
name="custom_message" value="" class="transfer-description" 
placeholder="Description" />';
echo $description_input;

此额外字段添加到的表单具有现有字段:“收件人”,“发件人”,“收件人”,“金额”,“令牌”和“提交”。

我不明白的是如何将这个额外的字段传递给ajax函数。

处理表单的.js文件的第一部分如下所示:

开发人员一直在帮助我,或者到目前为止我都做不到……他添加了'info'tinfo : info ,我认为这应该可以处理额外的字段。

jQuery(function($){
// Transfer function
var transfer_creds = function( to, creds, info, label ) {
$.ajax({
type : "POST",
data : {
action    : 'myc-transfer-creds',
sender    : myC.user_id,
recipient : to,
amount    : creds,
token     : myCRED.token,
tinfo     : info
},
dataType : "JSON",
url : myC.ajaxurl,
... (the rest of the .js here)

然后将多余的字段插入开发人员正在使用的日志条目中:

$transfer_message = '';
if ( isset( $_POST['tinfo'] ) )
$transfer_message = sanitize_text_field( $_POST['tinfo'] );

这不是完整的代码,但似乎是相关的部分。 所以,我的问题又是如何发送一个额外的表单字段到ajax,然后通过php发布该信息? 我不确定我是否已经正确解释了这一点,但感谢您的帮助。

首先,您需要获取必填字段的值,就像获取info的值一样

并将其添加到tinfo之后的数据参数中,例如:

tinfo     : info,
tcustom_message  : custom_message

您可以使用$_POST['tcustom_message ']访问此数据

我将按原样复制您的上述代码,并仅标记我认为对您有用的更改:

jQuery(function($){

var info = $('#custom_message').val(); //add this line to get your message

// Transfer function
var transfer_creds = function( to, creds, info, label ) {
$.ajax({
type : "POST",
data : {
action    : 'myc-transfer-creds',
sender    : myC.user_id,
recipient : to,
amount    : creds,
token     : myCRED.token,
tinfo     : info
},
dataType : "JSON",
url : myC.ajaxurl,

...(此处的其余.js)

让我知道这个是否奏效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM