我正在为Wordpress创建一个插件,我有一个运行完美的脚本,可以发出ajax请求,在数据库中写入一些数据。代码一直工作到在`` div中显示文本的部分passo2form
它最初为空,单击文本按钮后将插入其中。但数据没有记录在数据库中。我的代码如下所示:
主html:
<div class=\'principal-form\'>
<input type=\'text\' name=\'nome\' id=\'nome\' class=\'campo-form\' placeholder=\'Nome\' maxlength=\'50\'><br>
<input type=\'email\' name=\'email\' id=\'email\' class=\'campo-form\' placeholder=\'Email\' maxlength=\'120\'/>
<button type=\'submit\' id=\'enviarform\' class=\'botao-enviar\'>Efetuar Simulação</button>
</div>
<div id=\'passo2form\' class=\'passo2form\'></div>
运行的Javascript文件:
jQuery(\'#enviarform\').click(function(){
var nome = document.getElementById(\'nome\').value;
var email = document.getElementById(\'email\').value;
jQuery( "#passo2form" ).html("<div class=\'col-md-35 padding-top-15\'><div class=\'texto-ola\'><p>Olá <span class=\'cor-vermelho\'>" + nome + "</span>,</p><p>Estaremos enviando em breve sua cotação para o email <span class=\'cor-vermelho\'>" + email + " </span></p></div></div>");
var formData = {
\'nome\' : jQuery(\'input[name=nome]\').val(),
\'email\' : jQuery(\'input[name=email]\').val()
};
// process the form
jQuery.ajax({
type : \'POST\',
url : \'processa.php\',
data : formData,
dataType : \'json\'
})
.done(function(data) {
console.log(data);
});
});
我测试了
processa.php
文件,并完美地将数据插入数据库。但以下代码:
<?php
include_once($_SERVER[\'DOCUMENT_ROOT\'].\'/wordpress/wp-config.php\' );
global $wpdb;
$nome = trim($_POST[\'nome\']);
$email = trim($_POST[\'email\']);
$wpdb->insert(
wp_formclientes,
array(
\'nome\' => $_POST[\'nome\'],
\'email\' => $_POST[\'email\']
)
);
$wpdb->show_errors();
?>
SO网友:krunal sojitra
尝试使用此主HTML:
<form class="home_footer" method="post" id="home_conct_form" action="">
<input type="text" name="fname" class="txt" />
<input type="text" name="lname" class="txt" />
<input type="button" name="home_submit" id="home_submit" value="Get In Touch"/>
</form>
在同一页上添加:
<script>
$(\'#home_submit\').click(function(){
var formData = $("#home_conct_form").serialize()
$.ajax({
type: \'POST\',
url: \'<?php echo home_url(\'/\'); ?>wp-admin/admin-ajax.php?action=footerFrm\',
data: formData,
beforeSend: function(){
$(\'.progloader\').show();
$(\'#home_submit\').hide();
},
success:function(data) {
console.log(data);
}
});
});
</script>
在函数中添加blow代码。php
add_action(\'wp_ajax_footerFrm\' , \'footerFrm\');
add_action(\'wp_ajax_nopriv_footerFrm\' , \'footerFrm\');
function footerFrm() {
print_r($_POST);
die();
}