2011-08-03 3 views
0

나는 누군가가 나를 도울 수 있는지 궁금해.MySql에서 PHP를 통해 드롭 다운 메뉴 채우기

여러 테이블이 포함 된 MySql 데이터베이스가 있습니다. 제 질문은 세 가지의 데이터, 즉 'userdetails', 'detectors'및 'searchheads'의 사용에 관한 것입니다.

사용자가 지정한 '탐지기'를 보여주는 HTML 드롭 다운 메뉴를 채우고 다른 드롭 다운 메뉴를 다시 사용자에게 보여줄뿐 아니라 첫 번째 드롭 다운 메뉴에서 '탐지기'선택에 적용되는 '검색 헤드'.

세 테이블 모두 'userid'필드가 있으며 'detector'및 'search heads'테이블에는 'detectorid'필드가 있습니다.

어쩌면 누군가 내가 시작할 수있는 단서가 없다는 것을 인정해야하기 때문에 어떻게 설정해야하는지 알려주시겠습니까?

많은 감사와 인사말

크리스.

은 JS 코드를

<script type="text/javascript" language="javascript"> 
      $(document).ready(function(){  
      $.fetch_data = function (what) {   
      var send = {    
      "what" : what,    
      "data" : $("select[name=detectors]").val()   
      };   
      $.post ("dropdownmenus.php", send, function(data){    
      $("select[name=" + what + "]").html(data);   
      });  
      };  
      $.fetch_data ("detectors");  
      $("select[name=detectors]").live("change", function(){   
      // reset the searchhead as new data will be implemented   
      $("select[name=detectorsearchheads]").html("");   
      $.fetch_data ("detectorsearchhead");  
      }); 
      }); 
     </script> 

업데이트] PHP 코드

<?php  
$request = array (  
"detector"  => @$_POST["detector"],   
"detector"  => @$_POST["detector"] );  
// clean $request.  
$build_query = "select * from " . $request["what"] . " "      
. "where userid = \"" . $user_id . "\"";  
$build_results = mysql_query ($build_query);  
$return_results = "";  
foreach ($build_results as $index => $data) {   
$return_results .= "<option value=\"detector" . $data["id"] . "\">" . $data["text"] . 
"</option>";  }  
echo $return_results; ?> 

답변

1

이 복잡한 MySQL의 쿼리보다 더 많은 자바 스크립트입니다 업데이트.

form.php

양식이있는 곳이 페이지입니다. 필요한 데이터를 가져 오는 Ajax 함수를 만들었습니다. 나는 모든 것을 끝내지는 못했지만 완성해야 할 것에 대한 개요를주었습니다. select 입력에서 수집 된 데이터는 jQuery의 ajax post 요청을 통해 전송됩니다. 그런 다음 데이터가 조작되어 select 입력에 의해 이해 될 수있는 html 데이터로 형식화됩니다.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $.fetch_data = function (what) { 
     var send = { 
      "what" : what, 
      "data" : $("select[name=detectors]").val() 
     }; 
     $.post ("url/to/ajax.php", send, functio(data){ 
      $("select[name=" + what + "]").html(data); 
     }); 
    }; 
    $.fetch_data ("detectors"); 
    $("select[name=detectors]").live("change", function(){ 
     // reset the searchhead as new data will be implemented 
     $("select[name=searchhead]").html(""); 
     $.fetch_data ("searchhead"); 
    }); 
}); 
</script> 
<form> 
    <label>Detectors</label> 
    <select name="detectors"> 

    </select> 
    <label>Search Heads</label> 
    <select name="searchead"> 

    </select> 
</form> 

URL /로/ajax.php 여기

당신이 당신의 데이터를 조작하는 방법을 조정해야 할 것이다. 변수 what에 따라 PHP switch을 사용할 수 있습니다. 요청한 내용에 따라 쿼리가 변경됩니다.

<?php 
    $request  = array (
     "what"  => @$_POST["what"], 
     "data"  => @$_POST["data"] 
    ); 
    // clean $request. 
    $build_query = "select * from " . $request["what"] . " " 
        . "where userid = \"" . $user_id . "\""; 
    $build_results = mysql_query ($build_query); 
    $return_results = ""; 
    foreach ($build_results as $index => $data) { 
     $reurn_results .= "<option value=\"" . $data["id"] . "\">" . $data["text"] . "</option>"; 
    } 
    echo $return_results; 
?> 
+0

안녕하세요. 이것은 아주 훌륭합니다. 회신에 시간을 할애 해 주셔서 감사 드리며 내가 도왔던 모든 작업에 많은 감사를드립니다. 나는 이것에 가야하고, 내가 어떻게에 관해 알 수있게한다. 감사합니다 Chris – IRHM

+0

안녕하세요, 당신은 내 주위에 경험 부족이 용서해야 할 것입니다,하지만 나는 빨대에서 내가 무엇을 바꿔야 하는지를 파악해야만합니다. 나는 원래 게시물에 업데이트로 게시 한 미성년자 임에도 불구하고 약간의 변경 작업을 수행했습니다. 코딩의 모든 부분이 가능하다면 무엇을해야 할지를 설명해 주시겠습니까? 많은 감사와 인사 – IRHM

+0

런타임에 페이지의 정적 인 부분을로드하는 큰 팬이 아닙니다. 사용자는 사이트 속도를 느리게 할뿐만 아니라 사용자가 천천히 채워지는 빈 선택 상자로 처리됩니다. 플러스 검색 엔진은 자바 스크립트를 실행하지 않으므로 해당 내용을 볼 수 없습니다 (자바 스크립트가 비활성화 된 사용자도 마찬가지 임). – Blindy