I am creating a plugin for CMS Wordpress, in it I have a script that runs perfectly until the part of making the ajax request to write some data in the database. The code works until the part of displaying the text within div
passo2form
which initially is empty and after clicking the button the text is inserted inside it. However, the data is not written to the database. My code looks like this:
Main 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 file that runs:
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',
encode : true
})
.done(function(data) {
console.log(data);
});
});
I have tried the file'processa.php 'and it works perfectly by inserting the data into the database. But follow the code:
<?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();
?>