堆栈交换长时间侦听器,第一次调用。
我找到了关于开发人员的示例。wordpress网站,但我一直在努力。
本地化脚本:wp_localize_script()
在我的主题功能中。php文件,我有:
function wp_my_ajax_script() {
wp_localize_script( \'ajax_handle_agent_search\', \'myAjax\', admin_url( \'admin-ajax.php\' ));
wp_enqueue_script( \'ajax_handle_agent_search\' );
}
add_action( \'wp_enqueue_scripts\', \'wp_my_ajax_script\' );
在我的页面上,我添加了一个HTML代码wdiget,其中包含:
<script>
jQuery(document).ready( function() {
console.log("Document loaded");
jQuery("#searchButton").click( function(e) {
console.log("Search button clicked");
console.log("Ajax: " . myAjax);
e.preventDefault();
var zipCode = document.getElementById("inputField").value;
console.log("Zip code entered: " + zipCode);
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {
action: "zip_search_action",
zip_code : zipCode
},
success: function(response) {
if(response.type == "success") {
console.log("In success");
document.getElementById("results").html = response.data;
}
else {
console.log("In success, in else!");
}
},
error: function(errorThrown){
console.log("In error, error thrown!");
console.log(errorThrown);
}
})
});
});
</script>
<input type="text" id="inputField">
<input type="button" value="Search" id="searchButton">
然后我加载页面,在输入字段中输入邮政编码,然后单击按钮。开发人员工具控制台显示:
我已经为此工作了几周了,我在为Wordpress开发方面做得更好了,但web开发不是我的强项,所以当我觉得自己已经达到极限后,我会寻求帮助。任何能让我向前迈进的见解都将不胜感激。
提前感谢!
=======================================在1342 CST处编辑2020年3月12日
我已经将JS代码移动到主题JS目录中自己的文件中,权限为0755。然后,我在函数中添加了一个新函数。包含排队和本地化函数调用的php文件(如下所示)
function my_load_scripts() {
wp_enqueue_script( \'zip_js\', get_template_directory_uri() . \'/js/zip_search.js\' );
wp_localize_script( \'zip_js\', \'Zip_JS\', null);
}
add_action(\'wp_enqueue_scripts\', \'my_load_scripts\');
现在控制台显示:
====================================+++=在20年3月13日0806 CST编辑,我已经做得更进一步了。至少我相信是这样。下面是Wordpress中当前的代码,后面是控制台错误的屏幕截图。
在我的JS文件()中:
jQuery(document).ready( function() {
console.log("Document loaded");
jQuery("#searchButton").click( function(e) {
console.log("Search button clicked");
e.preventDefault();
var zipCode = document.getElementById("inputField").value;
console.log("Zip code entered: " + zipCode);
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {
action: "zip_search",
zip_code : zipCode
},
success: function(response) {
if(response.type == "success") {
console.log("In success");
//jQuery("#results").html(response.data);
document.getElementById("results").html = response.data;
}
else {
console.log("In success, in else!");
console.log(response);
}
},
error: function(errorThrown){
console.log("In error, error thrown!");
console.log(errorThrown);
}
});
});
});
在函数中。php这次包括我的DB查询:
function my_load_scripts() {
// Enqueue javascript on the frontend.
wp_enqueue_script(
\'zip_js\',
get_template_directory_uri() . \'/js/zip_search.js\',
array(\'jquery\')
);
// The wp_localize_script allows us to output the ajax_url path for our script to use.
wp_localize_script(
\'zip_js\',
\'myAjax\',
array( \'ajax_url\' => admin_url( \'admin-ajax.php\' ) )
);
}
add_action( \'wp_enqueue_scripts\', \'my_load_scripts\' );
function zip_search()
{
global $wpdb;
$output = \'\';
$zip = $_REQUEST[\'zipCode\'];
$query = \'SELECT county FROM Agent WHERE zip = %s\';
$result = $wpdb->get_var( $wpdb->prepare($query, $zip) );
$output .= "<p>";
$output .= $result;
$output .= "</p>";
$query = \'SELECT zip, county, zone, agent FROM Agent WHERE county = %s\';
$results = $wpdb->get_results( $wpdb->prepare($query, $result) );
$output .= "<ul>";
foreach( $results as $result )
{
$output .= "<li>".$result->zip." - ".$result->zone." - ".$result->agent."</li>";
}
$output .= "</ul>";
$result[\'type\'] = "success";
$result[\'data\'] = $output;
return json_encode($result);
die();
}
add_action(\'wp_ajax_zip_search_action\', \'zip_search\');
add_action( \'wp_ajax_nopriv_zip_search_action\', \'zip_search\' );
在我的Wordpress页面上:
<input type="text" id="inputField">
<input type="button" value="Search" id="searchButton">
控制台:
编辑:2020年3月13日上午11:52,CST添加工作代码。希望任何有类似问题的人都能看到我做了些什么,这会有所帮助JS:
jQuery(document).ready( function() {
console.log("Document loaded");
jQuery("#searchButton").click( function(e) {
console.log("Search button clicked");
e.preventDefault();
var zipCode = document.getElementById("inputField").value;
console.log("Zip code entered: " + zipCode);
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {
\'action\': "zip_search",
\'zip_code\' : zipCode
},
success: function(response) {
console.log(response.data);
if(response.success) {
console.log("response.type == success");
jQuery("#results").html(response.data.data);
}
else {
console.log("response.type == else");
console.log(response.data);
}
},
error: function(errorThrown){
console.log("In error, error thrown!");
console.log(errorThrown);
}
})
})
})
功能。php:
add_action(\'wp_ajax_zip_search\', \'zip_search\');
add_action(\'wp_ajax_nopriv_zip_search\', \'zip_search\' );
function zip_search()
{
global $wpdb;
$output = \'\';
$zip = $_REQUEST["zip_code"];
$query = \'SELECT county FROM Agent WHERE zip = %s\';
$result = $wpdb->get_var( $wpdb->prepare($query, $zip) );
$output .= "<p>";
$output .= $result;
$output .= "</p>";
$query = \'SELECT zip, county, zone, agent FROM Agent WHERE county = %s\';
$results = $wpdb->get_results( $wpdb->prepare($query, $result) );
$output .= "<ul>";
foreach( $results as $result )
{
$output .= "<li>".$result->zip." - ".$result->zone." - ".$result->agent."</li>";
}
$output .= "</ul>";
$response = array(
\'data\' => $output,
);
wp_send_json_success($response);
//return json_encode($response);
//die();
}
add_action( \'wp_enqueue_scripts\', \'my_load_scripts\' );
function my_load_scripts() {
// Enqueue javascript on the frontend.
wp_enqueue_script(
\'zip_js\',
get_template_directory_uri() . \'/js/zip_search.js\',
array(\'jquery\')
);
// The wp_localize_script allows us to output the ajax_url path for our script to use.
wp_localize_script(
\'zip_js\',
\'myAjax\',
array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) )
);
}
HTML:
<input type="text" id="inputField" placeholder="Enter zip code here">
<input type="button" value="Search" id="searchButton">