WordPress: Calling a php function out of javascript: ReferenceError: <function> is not defined

I am using Gravity Forms “after submission” event to call a javascript function out of the functions.php file.

There are 3 files involved: frontend-checklist.php, frontend-checklist.js and functions.php.

Read More

The GF Form is displayed in a lightbox and once the form is complete and the submit button is pressed, the GF even function pulls a specific field out of the GF form and passes it to the javascript function.

I am then configuring an Ajax call to insert/update a different database table with that information.

Everything seems to work, right up until the Ajax call, it never seems to call the php function to write the data to the database.

Here is the code out of my functions.php:

add_action('gform_after_submission_5', 'GF_Submit_5', 10, 2);

function GF_Submit_5($entry, $form){

?>
 <script>
    var vcl_c = <?php echo json_encode($entry['19']); ?>;
        frontend_checklist_fc_database_write(vcl_c );
 </script>  
<?php
}

The above code calls a javascript function. Here is that function:

function frontend_checklist_fc_database_write(fc_itemID) {

alert ("javascript - dbwrite" + fc_itemID);
    var data = { action: 'fc_database_write', fc_itemID: fc_itemID };
    jQuery.post(frontendChecklist.ajaxurl, data);

}

Finally, here is the code that defines the Ajax call to actually write the data, as well as the configuration of the action and the enqueue. This code is in a third file (outside of the javascript code and the functions.php files).

add_action('wp_ajax_fc_database_write', array('Frontend_Checklist', 'dbWriteAjax'));
add_action('wp_enqueue_scripts', array('Frontend_Checklist', 'add_js'));

function add_js() {
    wp_enqueue_script('frontend-checklist', plugins_url('frontend-checklist.js', __FILE__), array('jquery'));

    wp_localize_script( 'frontend-checklist', 'frontendChecklist', 
        array( 
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
        )
    );
}


public static function dbWriteAjax() {
    $user_ID = get_current_user_id();
    if ($user_ID == 0) die;
        $ca = (int)$_POST['fc_itemID'];
        $fc_completed = "'on'";

        $casql = $wpdb->prepare('SELECT completed from wp_fc_vendorlist WHERE fc_vendorID = ' .$user_ID. ' and fc_itemID = ' .$ca. ' ORDER BY fc_itemID ASC' );
        $cachk = $wpdb->get_row($casql, ARRAY_A);
        $table_name = 'wp_fc_vendorlist';
    if($cachk =='') {
        $wpdb->query($wpdb->prepare("INSERT INTO $table_name (fc_itemID, fc_vendorID, completed) VALUES ($ca, $user_ID, $fc_completed)"));
    } else {
        $wpdb->query($wpdb->prepare("UPDATE $table_name SET completed = $fc_completed WHERE fc_vendorID =  $user_ID  and fc_itemID = $ca"));
    }       


    die;
}

The Alert in the javascript will fire, and I can track the javascript code with firebug and it seems to be updating the variables correctly. The error : ReferenceError: frontend_checklist_fc_database_write is not defined presents itself as soon as the code drops into the javascript function.

Any help with this would be greatly appreciated.

Related posts

Leave a Reply