我正在创建一个wordpress插件(使用tinymce),其中我有一个按钮。在按钮上单击弹出模式显示一些列表框。期望从服务器接收列表框中的值。 所以要做到这一点 我有一个php文件,它与数据库建立连接并触发可以提供结果的查询。从客户端,我有一个js文件,我在其中编写ajax查询来调用php函数。 所以为了实现这一点,我正在编写一个函数,它将向php发出ajax查询。问题是我无法将ajax响应返回给调用者。
(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));
}
?>
答案 0 :(得分:0)
问题是你没有编码到AJAX可以理解的格式。为了在传输阵列时获得最佳效果,您需要使用json_encode。
因此,获取代码并进行微小修改并使用$ 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);
}