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 如何实验stm32主通道和互补通道独立输出
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题