2012-04-02 2 views
2

내 양식에 두 개의 자동 완성 필드가 있습니다. 나는다른 입력 값을 기반으로 jquery 자동 완성 SQL 쿼리 목록

$(function() { 
    var availableTags = [ 
     <?php for($k=0; $k<sizeof($arr_autoCompleteExerciseStr); $k++) {echo $arr_autoCompleteExerciseStr[$k]; } ?> 
    ]; 
    $("#inputExercise").autocomplete({ 
     source: availableTags, 
     minLength: 0 
    }); 
    $("#inputExercise").focus(function(){   
     $(this).autocomplete("search"); 
    }); 
}); 

다른는 mysql_query와 같은 코드가 다른 분야에 사용되는 ... ... 다음 코드를 사용하여

$result = mysql_query("SELECT * FROM exercise_strength"); 
$arr_autoCompleteExerciseStr=array(); 
$s=0; 
while($row = mysql_fetch_array($result)){ 
$autoCompleteExerciseStr = "\"".ucfirst($row['exercise'])."\", "; 
$arr_autoCompleteExerciseStr[$s] = $autoCompleteExerciseStr; 
$s++; 
} 

과 첫 번째 필드의 목록을 채 웁니다. 내가하고 싶은 일은 첫 번째 입력 내용을 기반으로 두 번째 필드의 목록을 변경하는 것입니다. 예를 들어, 사용자가 첫 번째 입력란에 Chest를 입력하면 my SQL 데이터베이스에서 선택된 두 번째 입력란에 관련 연습 목록이 표시됩니다.

가장 좋은 방법은 무엇입니까? 내가 페이지를 떠날 필요하지 않을 경우

나는

제발 도와주세요 .. 다음 사용자가 양식의 나머지 부분을 보충 할 필요가 원인이 선호! :)

- UPDATE는 - JSON에 대한 조언을 바탕으로

내 코드는 이제 다음과 같습니다.

스크립트 :

$(document).ready(function(){ 
    $.post('json_exercise.php', { /**/ }, showExercise, "text"); 
}); 

function showExercise(res){ 
    var list = JSON.parse(res); 
    $("#inputExercise").autocomplete({ 
     source: list, 
     minLength: 0 
    }); 
    $("#inputExercise").focus(function(){   
     $(this).autocomplete("search"); 
    }); 
} 

PHP 파일 :

$con = mysql_connect(***); 
if (!$con) {die('Could not connect: ' . mysql_error());} 
mysql_select_db("test", $con); 

$result = mysql_query("SELECT * FROM exercise_strength"); 
$i = 0; 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    { 
     //add the row to the $chat array at specific index of $i 
     $exercise[$i] = $row['exercise']; 
     $i += 1; 
    } 
echo json_encode($exercise); 

지금 난 그냥 PHP를 변경할 필요는 첫 번째 자동 완성 필드에서 선택한 뭐죠에 따라 제기했다.

답변

3

페이지를 떠나지 않고 SQL 쿼리의 결과를 기반으로 자동 완성하려면 AJAX 및 JSON을 조사해야 할 수 있습니다.

또한 DBMS와 PHP의 인터페이스를 위해 mysql_ * 함수 대신 PDO를 사용하십시오.

+0

고맙습니다. 새로운 질문으로 내 질문을 업데이트했습니다. 그것에 대해 의견이 있으십니까? – Virik

관련 문제