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.

dream3323
dream3323 不,我不知道functions.php不是自动的。正在加载和运行functions.php和frontend-checklist-js中的代码,我可以假设它们正在被正确加载吗?正在构建由frontend-checklist.php文件构建的页面,GF表单通过按下该表单上的按钮加载到灯箱中。我正在使用Web检查工具来获取上面引用的错误消息。
5 年多之前 回复
dongmao4486
dongmao4486 好的,但是你知道,与主题不同,使用插件时,不会自动包含functions.php文件,对吧?我猜他们建议你把它放在functions.php中,因为他们假设你会写一个主题,那个文件会被自动包含在内。而且,就像我说的,检查浏览器的网络检查工具,看看是否包含了javascript。这是弄清楚发生了什么的最好方法。
5 年多之前 回复
dongmeiran609914
dongmeiran609914 对不起,那部分可能有点不清楚。我正在写一个插件。GravityForms“提交后”事件代码必须放在functions.php文件中(根据他们的支持人员)。我编辑了上面的问题以涉及以下内容:涉及3个文件:frontend-checklist.php,frontend-checklist.js和functions.php。重力表格显示在灯箱中。
5 年多之前 回复
douzhun4124
douzhun4124 您似乎使用plugins_url('frontend-checklist.js',__FILE__)作为脚本的路径,但您只提到functions.php,这似乎表明您正在编写主题,而不是插件?您确定您的frontend-checklist.js正确导入页面吗?查看您喜欢的浏览器的调试工具。当然,如果不是,那将导致它不被定义。
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐