Javascript 使用php ajax js在模式弹出列表框中从服务器获取动态值。(tinymce wordpress)
我正在创建一个wordpress插件(使用tinymce),其中有一个按钮。单击按钮时,会出现一个带有一些列表框的弹出模式。列表框中的值预期将从服务器接收。 所以要做到这一点 我有一个php文件,它与数据库建立连接,并启动可以给出结果的查询。在客户端,我有一个js文件,在其中我编写了一个ajax查询来调用php函数。 因此,为了实现这一点,我正在编写一个函数,该函数将激发对php的ajax查询。问题是我无法将ajax响应返回给调用者Javascript 使用php ajax js在模式弹出列表框中从服务器获取动态值。(tinymce wordpress),javascript,php,ajax,wordpress,tinymce,Javascript,Php,Ajax,Wordpress,Tinymce,我正在创建一个wordpress插件(使用tinymce),其中有一个按钮。单击按钮时,会出现一个带有一些列表框的弹出模式。列表框中的值预期将从服务器接收。 所以要做到这一点 我有一个php文件,它与数据库建立连接,并启动可以给出结果的查询。在客户端,我有一个js文件,在其中我编写了一个ajax查询来调用php函数。 因此,为了实现这一点,我正在编写一个函数,该函数将激发对php的ajax查询。问题是我无法将ajax响应返回给调用者 (function() { tinymce.PluginMan
(function() {
tinymce.PluginManager.add('attach_thumbnail_button', function( editor, url ) {
editor.addButton( 'attach_thumbnail_button', {
title: 'Attach Thumbnail',
text: 'Attach Thumbnail',
onClick:
editor.windowManager.open({
title: 'Add Thumbnail',
body:[
{
type : 'listbox',
name : 'list_project',
label : 'Project Name',
values: get_project_list(list_project),
},
],
onsubmit: function(e){
displayThumbnail();
}
});
});
});
})();
function get_project_list(list_project){
jQuery.ajax({
type:"POST",
url: 'techpub/functions.php',
success: function (response) {
// i want to return the value in response as it will contain the values that i want to add in the list box.
//using return response; not giving me the desired result. the list box is empty.
}
});
}
function displayThumbnail(){
// this function is of no importance here
}
php文件如下所示
<?php
$myServer = "10.0.0.29";
$connectionInfo = array( "Database"=>"database", "UID"=>"app", "PWD"=>"app");
// Connect using SQL Server Authentication.
$conn = sqlsrv_connect($myServer, $connectionInfo);
if ( $conn )
{ //this is some query that will send values as response
$query = "select column_name from table";
$stmt = sqlsrv_query( $conn, $query);
if ( $stmt )
{
while( $row = sqlsrv_fetch_array( $stmt))
{
echo $row ;
}
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
}
else
{
echo "Connection not established";
die( print_r(sqlsrv_errors(), true));
}
?>
问题在于您没有按照AJAX能够理解的格式编码。为了在传输数组时获得最佳效果,您需要使用json_编码 因此,对代码进行细微修改,并使用$tempArray作为变量来突出显示将发送到浏览器的内容:
if ( $conn )
{
$query = "select column_name from table";
$stmt = sqlsrv_query( $conn, $query);
if ( $stmt )
{
/* If you are going to go through the results one by one */
$tempArray = array();
while( $row = sqlsrv_fetch_array( $stmt))
{
$tempArray[] = $row;
}
/* Output the results */
echo json_encode($tempArray);
}
tempArray中的值未填充到浏览器模式弹出窗口的列表框中。。下面的函数get_project_list必须将ajax响应返回到列表框。警报(响应)工作正常,但列表框仍然为空。。函数get_project_list(){jQuery.ajax({type:“POST”,url:'techpub/modelist.php',success:function(response){//tinymce.activeEditor.insertContent(response);alert(response);return response;});