0

I am creating a plugin for Wordpress, I have a script that runs perfectly to part to make ajax request to write some data in the database. The code works until the part to display the text within the `` divpasso2form which is initially empty and after clicking on the text button is inserted into it. But the data are not recorded in 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'
})
    .done(function(data) {

        console.log(data); 

    });

});

I tested the processa.php file and works perfectly making the insertion of data in the database. But the following 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();

?>
1
  • 2
    please first google and read how to properly write ajax in wordpress. It will probably solve this question and prevent you from doing additional mistakes later Commented Sep 29, 2016 at 16:01

2 Answers 2

1

Try like this Main 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>

Add on same page:

<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>

Add blow code in Function.php

add_action('wp_ajax_footerFrm' , 'footerFrm');
add_action('wp_ajax_nopriv_footerFrm' , 'footerFrm');
function footerFrm() {
   print_r($_POST);
   die();
}
0

That not really the right to do it with WordPress (wp_localize_script, admin_url, wp_ajax_no_priv...).

But with you method, it's easy to adapt:

Instead of include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-config.php' );

Add this at the top of processa.php

define('WP_USE_THEMES', false);
require ('../wp-blog-header.php'); // be carrefull to get your right path

You file can now use WordPress globals, functions...

6
  • But the problem was not this. processa.php works if the form of action is directed to it without ajax. I'm not managing to create ajax within my plugin in the right way that Wordpress requires.
    – Wendell
    Commented Sep 29, 2016 at 16:23
  • it is probably very wrong to put a php file at wordpress root, it can not be part of a plugin or theme. IMO this method should be used only when integrating with 3rd party code, not when enhancing wordpress functionality Commented Sep 29, 2016 at 16:24
  • ..... you don't even know where wp-blog-header.php is in the general case Commented Sep 29, 2016 at 16:26
  • that why I told @Wendell that it's not the clean way to do it. The best is to put your code in a callback function handle by a js files that can be detect within a wp_ajax or wp_ajax_nopriv action.
    – Benoti
    Commented Sep 29, 2016 at 16:34
  • 1
    I am ideologically against suggesting to people to do what i think is wrong ;) even if it can be made to work :) Commented Sep 29, 2016 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.