【发布时间】:2016-09-01 17:06:00
【问题描述】:
所以,我正在开发一个利用 jquery 和 mysql 动态更新下拉框的插件。
当页面第一次加载时,下拉框应该填充从 mysql 中选择的数据。但是,除了渲染到页面的空下拉框之外,没有任何东西可以工作。并且不会发出错误消息。
我在这里俯瞰什么?
插件/myplugin/myplugin.php
<?php
/**
* Plugin Name: Test
* Plugin URI:
* Description: This plugin performs dynamic region updates into select boxes in WordPress
* Version: 1.0.0
* Author: Me
* Author Email:
* License: GPL2
*/
function getregions_scripts() {
wp_enqueue_script(
'getregions-script',
plugin_dir_url(__FILE__) . "assets/getregions.js",
array('jquery'),
'1.0',
true
);
wp_localize_script(
'getregions-script', // this needs to match the name of our enqueued script
'gymRegions', // the name of the object
array('ajaxurl' => admin_url('admin-ajax.php')) // the property/value
);
}
add_action( 'wp_enqueue_scripts', 'getregions_scripts' );
add_action( 'wp_ajax_showcountries', 'showcountries_callback' );
add_action( 'wp_ajax_no_priv_showcountries', 'showcountries_callback' );
function showcountries_callback() {
include_once("pdo_mysql.php");
pdo_connect("localhost","user","password");
pdo_select_db("wpdb");
$action=$_POST["action"];
if($action=="showcountries"){
$showcountry = pdo_query("Select country_data from wp_usertable");
if (!$showcountry) {
$message = 'Invalid query: ' . pdo_error() . "\n";
$message .= 'Whole query: ' . $showcountry;
die($message);
}else{
foreach($showcountry as $row){
echo '<option value=".$row[country_code].">.$row[country_name].</option>';
}
}
}
else if($action=="showregions"){
$country_id= $_POST["country_id"];
$showregion = pdo_query("Select region_code, region_name from regiontbl
WHERE country_id=?", pdo_real_escape_string($country_id));
if (!$showregion) {
$message = 'Invalid query: ' . pdo_error() . "\n";
$message .= 'Whole query: ' . $regionquery;
die($message);
}else{
foreach($showregion as $row){
echo '<option value=".$row[region_code].">.$row[region_name].</option>';
}
}
}
}
function showcountries_frontend() {
$the_html = '
<form id="MyForm">
<div style="float: left">
<select id="CountryList" onchange="getRegion()" size="20"></select>
<select id="RegionList" size="20" onchange="getMap()"></select>
</div>
<div id="cityList" style="float: right"></div>
</form>';
return $the_html;
}
add_shortcode("sc_frontend", "showcountries_frontend");
?>
plugins/myplugin/assets/getregions.js
function initialize($) {
.......
feedData($);
}
jQuery(document).ready(function ($) { initialize($); });
function feedData($) {
jQuery(document).ready(function ($) {
var serialized = $('#MyForm').serialize();
$.ajax({
cache: false,
type: "POST",
async: false,
url: "gymRegions.ajaxurl",
data:{action=showcountries, serialized},
success: function (data) {
$('#CountryList').append(data);
},
error: function (data, status, error) {
console.log(data);
console.log(status);
console.log(error);
}
});
});
}
【问题讨论】:
-
您的数据是否从您的 ajax 调用中返回?在
success之后尝试console.log(data)并告诉我们。 -
@jbrya029 不,没有什么可以恢复。我在success:中放置了一个console.log(data),但没有生成任何输出。有什么想法吗?
-
您的问题可能与您的 pdo 连接、您的“action”后置变量或您的 echo 语句引发错误有关。您是否尝试过在 Chrome 的开发者工具 --> 网络选项卡中查看 XHR 请求?如果你抛出一个 PHP 错误,它应该出现在这里,而它可能不会在 ajax 调用中返回为“数据”。
-
@jbrya029 我在 Chrome 中查看了 XHR 请求,同样,那里也没有生成任何内容。我只在开发人员工具中收到“无效的速记属性初始化程序”错误通知 --> 来源。
-
尝试将
else语句添加到您的$actionif/elseif 以回显错误消息。如果您仍然一无所获,您可能只需要回显文件的每一步,直到找到断点并从那里解决问题。
标签: javascript php jquery mysql wordpress