Javascript 表单未通过Ajax从wordpress站点提交到数据库
以下是弹出窗口中的表格,我无法将数据提交到数据库。请帮我解决这个问题Javascript 表单未通过Ajax从wordpress站点提交到数据库,javascript,jquery,ajax,wordpress,Javascript,Jquery,Ajax,Wordpress,以下是弹出窗口中的表格,我无法将数据提交到数据库。请帮我解决这个问题 <div id="fsModal" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <button type="button
<div id="fsModal" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<button type="button" id="cross-btn" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<div id="desk-view" class="modal-body">
<div id="pop-up" class="row">
<div class="arrow_box side-heading col-xs-5 col-sm-5">
<h3>Join the Movement, the newsletter that tackles the justice issues that matter to you most.</h3>
</div>
<div class="right-form col-xs-7 col-sm-7">
<form id="form-search" action="" method="POST" class="form-inline subscribe-form">
<div class="form-group">
<input id="email-form" type="email" class="email-popup" placeholder="Enter your email" name="email">
</div>
<a href="#" id="submit-form" class="btn-form">Sign up</a>
</form>
</div>
</div>
</div>
</div>
</div>
我直接进入模板页面,到newsletter_insert.php提交表单。我的模板php代码如下
$email=$_POST['email'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['user_email']) && !empty($_POST['user_email'])) {
$wpdb->insert('newsletter_table', array(
"emailaddress" => $email
));
echo "1";
}else{
echo "2";
}
}
if (data == 1) {
alert("Subscribed Successfully!!");
} else if(data == 2){
alert("Something Went Error");
}
用于将数据字符串化为JSON,然后从ajax传递,如下所示,而不是数据:{'user_email':$(“#email form”).val(),
还要确保在ajax请求中添加contentType:“application/json”
data: JSON.stringify(data),
在wordpress Preference function.php文件中,使用wp_ajax方法: 如下图所示: 您的jquery代码来自wp中的任何文件
jQuery.ajax({
url: "http:your_host_name/wp-admin/admin-ajax.php",
type: 'POST',
dataType: 'json',
data:{
action: 'your_action_name',
},
success : function(response){
console.log(response);
}
});
在functions.php文件中写入:
add_action("wp_ajax_your_action_name", "test_function");
function test_function(){
echo "Here";
}
这样ajax在wordpress中工作
$('#submit-form').click(function(e) {
$.ajax({
data:{'user_email':$("#email-form").val()},
type: "POST",
url: "http://localhost/xxxx/index.php/news-letter/",
success: function(data){
if (data == 1) {
alert("Subscribed Successfully!!");
} else if(data == 2){
alert("Something Went Error");
}
}
});
});
php代码是
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['user_email']) && !empty($_POST['user_email'])) {
$wpdb->insert('newsletter-table', array(
"emailaddress" => $_POST['user_email'],
"date" => date('Y-m-d')
));
echo "1";
}else{
echo "2";
}
}
您收到的HTTP错误是什么?我没有收到任何错误,但是表单没有提交。我遗漏了一些内容,请查找更新的答案
var data={'user_email':$(“#email form”).val()代码>只需更改disno,它不工作,我想错误在php文件中,我包括您的调试命中php代码?dis是否正确我对此有疑问$email=$\u POST['email']
应该是$email=$\u POST['user\u email']代码>?
$('#submit-form').click(function(e) {
$.ajax({
data:{'user_email':$("#email-form").val()},
type: "POST",
url: "http://localhost/xxxx/index.php/news-letter/",
success: function(data){
if (data == 1) {
alert("Subscribed Successfully!!");
} else if(data == 2){
alert("Something Went Error");
}
}
});
});
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['user_email']) && !empty($_POST['user_email'])) {
$wpdb->insert('newsletter-table', array(
"emailaddress" => $_POST['user_email'],
"date" => date('Y-m-d')
));
echo "1";
}else{
echo "2";
}
}