2014-03-25 2 views
0
내가 첫번째 PHP를 사용하여 로컬 디렉토리에서 파일을 읽을 자동 완성 스크립트 작업입니다

와 자동 완성은 아래와 같이PHP - PHP와 자바 스크립트

<?php 
$file = glob('pages/*'); 
//var_dump($file); 

foreach($file as $value) 
{ 
    $output = substr($value, 6, strlen($value) - 6); 
    //echo($output)."<br>"; 
} 
?> 

위의 스크립트는 '페이지에있는 모든 파일을 표시합니다 '폴더 즉, pageone.html, pagetwo.html ....

그런 다음 javascript 파일을 사용하여'page '에 입력 할 때'pageone.html '또는'pageone.html '과 같은 자동 완성 옵션이 표시되어야하는 텍스트 필드를 표시합니다. 'pagetwo.html'등

<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<title>jQuery UI Autocomplete - Default functionality</title> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> 
<script src="//code.jquery.com/jquery-1.9.1.js"></script> 
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> 
<link rel="stylesheet" href="/resources/demos/style.css"> 
<script> 
$(function() { 
var availableTags = [" 
<?php echo $output; ?>" 
]; 
$("#tags").autocomplete({ 
source: availableTags 
}); 
}); 
</script> 
</head> 
<body> 
<div class="ui-widget"> 
<label for="tags">Tags: </label> 
<input id="tags"> 
</div> 
</body> 
</html> 

위의 PHP 코드를이 js와 결합하여 단일 PHP 파일

과 같이 표시합니다. '$ output'을 js 'availableTags'변수에 포함 시키려고 시도하지만 텍스트 필드에 아무 것도 입력하지 않으면 무슨 일이 일어날 지 모르겠다. js에 임베디드 된 PHP 코드와 관련이 있기 때문에 도움이된다.

답변

1

$output에는 하나의 값 (목록의 마지막 파일) 만있다.

<script> 
$(function() { 
var availableTags = <?=json_encode($res)?> 
... 
+0

은 매력처럼 작동합니다. – AS17

1

당신은 autocomplete.js을 사용할 수 있습니다 (json_encode 기능) 자바 스크립트 배열 : 당신은 같은 파일의 배열을 만들 수 있습니다

$res = array(); 
foreach($file as $value) 
{ 
    $res[] = substr($value, 6, strlen($value) - 6); 
} 

을하고 자바 스크립트로 전달합니다. jQuery UI를 사용하는 것이 훨씬 가볍고 사용하기 쉽습니다.

관련 문제