2016-08-24 1 views
-1

나는 버튼이있는 wordpress plugin (tinymce 사용)을 만들고있다. 버튼을 클릭하면 일부 목록 상자에 팝업 모달이 나타납니다. 목록 상자의 값은 서버로부터 수신 될 것으로 예상됩니다. 그래서 이것을 달성하기 위해 나는 데이터베이스와 연결하고 결과를 줄 수있는 쿼리를 발생시키는 PHP 파일을 가지고있다. 클라이언트 측에서, 나는 PHP 함수를 호출하는 아약스 쿼리를 작성하는 js 파일이 있습니다. 그래서 이것을 달성하기 위해 PHP로 아약스 쿼리를 발생시키는 함수를 작성하고 있습니다. 문제는 호출자에게 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는 목록 상자에 아약스 응답을 반환해야합니다. 경고 (응답) 잘 작동하지만, 목록 상자는 여전히 비어 .. 기능 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}); –