dsc6517 2015-01-04 16:37
浏览 89

Wordpress:用javascript调用php函数:ReferenceError:<function>未定义

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.

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.

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog