2016-08-24 6 views
-1

私はボタンがあるwordpressプラグイン(tinymceを使用して)を作成しています。ボタンをクリックすると、いくつかのリストボックスにポップアップモーダルが表示されます。リストボックスの値はサーバーから受信されることが期待されます。 これを達成するには 私はデータベースに接続し、結果を与えることができるクエリを起動するphpファイルを持っています。クライアント側から、私はPHP関数を呼び出すajaxクエリを記述しているjsファイルを持っています。 これを達成するために、私はphpにajaxクエリを発生させる関数を書いています。問題は、呼び出し元にajaxレスポンスを返すことができないことです。php ajax jsを使用して、モーダルポップアップリストボックス内のサーバからの動的値。 (tinymce-wordpress)

(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

問題は、AJAXを理解するであろう形式にエンコードされていないです。配列を送信するときの最良の結果を得るには、json_encodeを使用します。

だから、あなたのコードを取り、わずかな変更を行うと、ブラウザに送信取得されるかを強調するための変数として$ tempArrayを使用して: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); 
} 
+0

値は、リストボックスに移入されていませんブラウザのモーダルポップアップ.. 以下の関数get_project_listは、リストボックスにajaxレスポンスを返す必要があります。警告(レスポンス)が正常に動作しますが、リストボックスは空のまま..です 関数get_project_list(){ \t jQuery.ajax({ \t \tタイプ: "POST"、 \t \t URL:「techpub/modellist.php 」 \t \t成功:関数(応答){ \t \t \t //tinymce.activeEditor.insertContent(応答); \t \t \tアラート(応答); \t \t \tリターンレスポンス。 \t \t} \t \t \t}); –

関連する問題