我正在尝试构建一个wordpress插件来提供一个过滤表单,它使用Ajax从用户发送的输入中获取数据库中的数据。我正在尝试通过Ajax提交表单。
我怀疑是否放置了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\');
?>
表单UI
schoolform_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位于下,但在控制台日志中显示语法错误。