skip to Main Content

I try to submit a form using Ajax, working in a plugin. I developed two plugins the first one was working but not anymore and I didn’t find any mistakes. I think that is not coming from the code it self but I’m a bit lost.

Here is my php :

wp_enqueue_script( 'b_form', plugin_dir_url( __FILE__ ) . 'js/b-form.js', array( 'jquery') );
wp_localize_script( 'b_form', 'ajax_url', admin_url( 'admin-ajax.php' ) );
include plugin_dir_path( __FILE__ ) . 'pages/b-form.php';

and my js :

var fd = new FormData( document.getElementById( 'b_form' ) );
fd.append( 'action', 'b_generate_comparison' );
$.ajax({
     type: "POST",
     url: ajax_url,
     data: fd,
     contentType: false,
     processData: false,
        })
.done( function( response ) {
...
}
.fail( function( response ) {
...
}
.always( function( response ) {
...
}

2

Answers


  1. Chosen as BEST ANSWER

    I was posting files and there were to big,

    So in /etc/php/7.0/apache2/php.ini increase

    upload_max_filesize = 13M
    post_max_size = 27M
    

  2. Try below code and if it couldn’t work then check following things –
    Change URL in below code –

    var fd = new FormData( document.getElementById( 'b_form' ) );
    fd.append( 'action', 'b_generate_comparison' );
    $.ajax({
         type : 'POST',
         url : 'http://YOUR_SITE_URL/wp-admin/admin-ajax.php',
         dataType : 'json',
         data: fd,
         contentType: false,
         processData: false,
    success : function( response ) {        
    .done( function( response ) {
    ...
    }
    .fail( function( response ) {
    ...
    }
    .always( function( response ) {
    ...
    }
    } // success 
     });    // Ajax request end
    

    Checklist

    1. AJAX URL ( for wordpress default is
      your_site_url/wp-admin/admin-ajax.php)
    2. DataType could be ‘json’,
      ‘html’, ‘xml’ Check what server is delivering you though your
      request

    Hope this could help. Happy Coding.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search