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 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条