动态值从服务器在模态弹出列表框使用PHP ajax js.(tinymce-wordpress)


dynamic values from server in modal popup listbox using php ajax js. (tinymce-wordpress)

我正在创建一个wordpress插件(使用tinymce),其中我有一个按钮。在按钮上单击一个弹出模式出现与一些列表框。列表框中的值预计将从服务器接收。为了达到这个目的我有一个PHP文件,使与数据库连接,并触发查询,可以给出结果。从客户端,我有一个js文件,我正在写一个ajax查询调用php函数。因此,要实现这一目标,我正在编写一个函数,将触发ajax查询php。问题是我不能返回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));
}
?>  

问题是您没有编码成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);
}