AJAX File Upload to PHP after AJAX Complete – Missing Link

It is a WordPress installation using Gravity Forms which I know pretty well, however what I am trying to do is get the value of a hidden input and use it in a PHP script to get more data about.

JS CODE: http://pastebin.com/5FFf4ESb

Read More
jQuery(function($){

$(document).bind('gform_post_render', function(event, form_id){

    if (form_id == 1) {


        var uploader = $("#gform_drag_drop_area_1_1");

    var uploads = $("#gform_uploaded_files_1").val();

     alert(uploads);

    uploader.on("drop", function() {  

    $.ajax({
    url: mp3_object.ajaxurl,
    async: true,
    type: "POST",
    data: {
    action : 'ajax_mp3',
    files : uploads, 
    },

    success: function(response) {
        //results = jQuery.parseJSON(response);
        alert(response);
    }
});

});




        }



    });



});

PHP CODE: http://pastebin.com/H2Cd1Dfd

function enqueue_mp3_scripts_init($form, $is_ajax) {
     if ( $is_ajax ) {
    wp_enqueue_script( 'mp3-script', plugin_dir_url(__FILE__) . 'js/scripts.js', array('jquery'), 1.0 ); // jQuery will be included automatically
    wp_localize_script( 'mp3-script', 'mp3_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
     }
    }

add_action('gform_enqueue_scripts', 'enqueue_mp3_scripts_init', 10, 2);

function ajax_mp3_func() {


    if( isset($_POST['files']) )  {

    echo $_POST['files'];               
    }

die(); // stop executing script
}

add_action( 'wp_ajax_ajax_mp3', 'ajax_mp3_func' ); // ajax for logged in users
add_action( 'wp_ajax_nopriv_ajax_mp3', 'ajax_mp3_func' ); // ajax for not logged in users

The issue I am having is that the file upload uses plupload AJAX request and it is performing asynchronously with my JS code so my code fires before the value of the hidden field var uploads is set and it returns empty before being sent via my AJAX request to the PHP File.

I don’t know enough about AJAX to know how to isolate and run my AJAX request after the hidden value is set.

Related posts