2012-09-22 13 views
0

AJAX 및 간단한 HTML DOM 파서를 사용하여 자동 완성 스크립트를 만들려고합니다.AJAX 및 간단한 HTML DOM 파서를 사용하여 자동 완성

PHP 파일은 (discogs.php) : http://jsfiddle.net/Mobius1/cYGWs/

내가 아는 방법을 작성하는 방법 :

<?php 

    ini_set('user_agent', 'TEST/1.0 +http://127.0.0.1/test/'); 

    require_once('simple_html_dom.php'); 

    // Create DOM from URL 
    $html = file_get_html('http://www.discogs.com/release/'.$_POST["album_id"]); 

    // Grab the Label 
    $label = $html->find('.content > a', 0); 
    echo $label; 

    // Grab the Catalogue Number 
    $catno = $html->find('.content', 0); 
    echo $catno; 

    // Grab the tracklist 
    $tracklist = $html->find('div#tracklist', 0);  
    echo $tracklist; 

?> 

내가 여기에 HTML 및 jQuery를 넣어 한 공간을 절약하기를 여기에 지금까지 가지고 무엇을 단일 텍스트 영역/$('element').val(data) 입력을 사용하여 입력,하지만 PHP 파일 ($label, $catno$tracklist)에서 세 변수를 모두 세 입력을 작성하는 방법을 알아낼 수 없습니다.

가능합니까? 그런 다음

jQuery.post('discogs.php', jQuery("#myform").serialize(), function(data) { 

$('element').val(data); 

       },"json"); 

아래와 같이 스크립트에

+0

그런데 왜 [Discogs API] (http : //www.disc ogs.com/developers/) DOM을 구문 분석하는 대신? PHP 터널링이 필요하지만, 그것은 클라이언트에 달려 있습니다. – moonwave99

+0

대단히 고맙습니다. 사용 해보겠습니다. –

답변

0

최초의 메이크업 변화, 당신의 PHP 스크립트를 변경 & 다음과 같이하면

// New line of code for json 
ini_set('user_agent', 'TEST/1.0 +http://127.0.0.1/test/'); 
require_once('simple_html_dom.php'); 

    // Create DOM from URL 
    $html = file_get_html('http://www.discogs.com/release/3894873'); 

    // Grab the Label 
    $label = $html->find('.content > a', 0); 

    // Grab the Catalogue Number 
    $catno = $html->find('.content', 0); 

    // Grab the tracklist 
    $tracklist = $html->find('div#tracklist', 0); 


$data = array (
"label" => (string) $label, 
"catno" => (string) $catno, 
    "tracklist" => (string) $tracklist, 
); 

echo json_encode($data); 
exit; 

그런 변수 문을 에코 제거,

다음과 같이 당신의 성공 핸들러 함수를 변경
function(data) { 

$('lable element id').val(data.label); 
$('catno element id').val(data.catno); 
$('tracklist element id').val(data.tracklist); 

       } 
+0

좋아, 모든 편집을 완료했지만 입력 후에는 실행 후 모두'[object Object]'가 있습니다. –

+0

새롭게 업데이트 된 바이올린 및 PHP 코드를 볼 수 있습니까? – GBD

+0

여기 있습니다 : http://jsfiddle.net/Mobius1/wdmjs/ –

관련 문제