gpt4 book ai didi

javascript - 将表单数据和操作发送到 wordpress admin-ajax.php

转载 作者:行者123 更新时间:2023-11-29 23:17:27 27 4
gpt4 key购买 nike

我有一个表单,我想将其数据发送到 admin-ajax:

<form method="POST" id="form">
<input type="text" name="name">
<input type="number" name="phone">
<input type="email" name="email">
<textarea name="overview"></textarea>
<input type="submit" name="submit" id="submit" value="Submit">
</form>

使用 Ajax 发送数据的 Javascript/jQuery 代码:

document.getElementById('submit').addEventListener('click', function(e){
e.preventDefault();
var form_data = $('#form').serialize();
$.ajax('http://mywebsite.com/wordpress/wp-admin/admin-ajax.php', {
method: "POST",
data: form_data + {action: 'my_action'},
success: function (response) {
console.log(response);
},
error: function (err) {
console.log('Error:' + err);
}
});
});

还尝试了formData:

var form_data = new FormData();
form_data.append('form', $('#custom').serialize());
form_data.append('action', 'my_action');

如何发送表单数据和 Action my_action

最佳答案

您需要将此行从 data: form_data + {action: 'my_action'}, 更改为 data: {action: 'my_action', form_data:form_data},

jQuery(document).on("click","#submit", function(){
e.preventDefault();
var form_data =jQuery('#form').serializeArray();
jQuery.ajax('http://mywebsite.com/wordpress/wp-admin/admin-ajax.php', {
method: "POST",
data: {action: 'my_action', form_data:form_data},
success: function (response) {
console.log(response);
},
error: function (err) {
console.log('Error:' + err);
}
});
});

并将输入类型提交更改为按钮

关于javascript - 将表单数据和操作发送到 wordpress admin-ajax.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233328/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com