在WordPress插件下未调用AJAX功能

时间:2016-12-13 作者:Himani

我正在尝试构建一个wordpress插件来提供一个过滤表单,它使用Ajax从用户发送的输入中获取数据库中的数据。我正在尝试通过Ajax提交表单。

Plugin File Structure

我怀疑是否放置了AJAX调用函数,form_ajax_process_request() 在下面ajax-process-form.js 是否正确?

这是ajax-process-form.js 密码

jQuery(document).ready( function($) {
jQuery(\'HCS-quicksearchForm\').bind(\'submit\', function() {

var data = {
    action: \'form_response\',
    post_var: \'this will be echoed back\'
};
console.log(the_ajax_script.ajaxurl);
$.post(the_ajax_script.ajaxurl, data, function(response) {
    alert(response);
});
return false;
});
});

<?php 
    function form_ajax_process_request() {
        if( isset( $_POST["post_var"] ) ) {
            $response = $_POST["post_var"];
            echo $response;
            die();
        }
    }

    add_action(\'wp_ajax_form_response\',\'form_ajax_process_request\');
    add_action(\'wp_ajax_nopriv_form_response\',\'form_ajax_process_request\');
?>
表单UIschoolform_view.php 密码

<?php
    echo \'<fieldset>\';
    echo\'<div id="HCS-quicksearch" class="HCS-quicksearchWrapper">\';
    echo\'<form id="HCS-quicksearchForm" class="hcs-school-form" action="" method="POST">\';      
    echo \'<div id="HCS-qsCityWrap" class="HCS-qsFieldWrap">\';
    global $wpdb;
    $city_results = $wpdb->get_results ("SELECT * from wp_gj73yj2g8h_hillsborough_county_cities;"        );
    echo \'<select id="city_id HCS-qsCity" name="city_name" class="HCS-qsInput HCS-qsSelectInput">\';
    echo \'<option value="">Choose a City</option>\';
    foreach ( $city_results as $result ) {
    echo \'<option>\'.$result->city_name.\'</option>\';
    }
    echo \'</select>\';
    echo \'</div>\';
    echo \'<input id="HCS-qsSubmit" class="HCS-qsInput HCS-qsButtonInput" type="submit" name="form-submitted" value="Search">\';      
    echo \'</form>\';
    echo \'</div>\';
    echo \'</fieldset>\';
?>
插件core school\\u core。PHP代码

    <?
 /*
Plugin Name: Hillsborough County Homes By School Properties
Plugin URI: http://accessrealtytampa.com
Description: Search For Homes By School Name, Ranking and other Features
Version:0.1.0
Author: Himani Lotia

*/
define( \'HCS_URL\', plugin_dir_url( __FILE__ )   );
define( \'HCS_PATH\', plugin_dir_path( __FILE__ ) );
define( \'HCS_VERSION\', \'0.1.0\'                  );

add_action(\'init\', \'register_script\');
function register_script() {
    wp_register_style( \'hcs-style\', HCS_URL . \'/assets/css/hcsstyle.css\', FALSE, HCS_VERSION, \'all\' );
}

add_action(\'wp_enqueue_scripts\', \'enqueue_style\');

function enqueue_style() {
    wp_enqueue_style(\'hcs-style\');
}

function ajax_load_scripts() {
    wp_enqueue_script("ajax-process-form", HCS_URL. \'ajax-process-form.js\', array( \'jquery\' ) );
    wp_localize_script ( \'ajax-process-form\', \'the_ajax_script\', array(\'ajaxurl\' => admin_url(admin-ajax.php ) ) );
}

add_action(\'wp_print_scripts\', \'ajax_load_scripts\');

function schoolform() {

    enqueue_style();
     $saved_data =  get_option(\'save_plugin_settings\');
     $saved_data = $saved_data ? unserialize($saved_data) : null ;

     include_once(HCS_PATH . \'/schoolform_view.php\');
}


function hcs_shortcode() {
    ob_start();
    schoolform();
    return ob_get_clean();
}

add_shortcode(\'hcs_school_filter\',\'hcs_shortcode\');
?>
在我的google chrome控制台中,ajax处理表单。js位于下,但在控制台日志中显示语法错误。

1 个回复
SO网友:Benoti

语法错误来自包含php代码的js文件。您不需要将此部分放在js文件中,而需要放在php文件中。

<?php 
function form_ajax_process_request() {
    if( isset( $_POST["post_var"] ) ) {
        $response = $_POST["post_var"];
        echo $response;
        die();
    }
}

add_action(\'wp_ajax_form_response\',\'form_ajax_process_request\');
add_action(\'wp_ajax_nopriv_form_response\',\'form_ajax_process_request\');
?>
此部件需要在school_core.php (无<?php and ?>).

相关推荐

尝试在WordPress中实现AJAX注释,遇到WP错误

我试图在WordPress中为我的评论实现Ajax,使用this tutorial. 但我在将教程中的代码集成到自己的预构建主题时遇到了问题。问题是,我要么得到一个WP错误“检测到重复注释;看来你已经说过了!”或标准500错误。以下是我得到的:下面是我对ajax的评论。js文件如下所示: * Let\'s begin with validation functions */ jQuery.extend(jQuery.fn, { /* * check i