ajax를 이용하여 DB값 출력질문이요.

ajax를 이용하여 DB값 출력질문이요.

QA

ajax를 이용하여 DB값 출력질문이요.

답변 2

본문

ajax가 익숙치 않아서 구글링하면서 찾아본 소스 뜯어서 수정중인데요.. 

이동통신사 셀렉트박스를 SKT로 바꾸면 
SKT에 저장된 DB 값들을 제조사명에 뿌려주고 싶은데요.. 
크롬 개발자도구에서 콘솔 확인 해본 결과
콘솔창에는 DB값이 잘 넘어오는데 제조사 select박스에는 출력이 안됩니다.
그냥 [] 이렇게만뜨네요..뭐가문젠지 봐주세요 ㅠㅠ

<? 
include "./db_connect.inc"; 
$tel=$_REQUEST['tel']; 
$make=$_REQUEST['make']; 

$sql = "SELECT * FROM wellphone1 where tel='$tel' ORDER by num asc "; 
$result = mysql_query($sql); 
?> 

<select name="make"> 
<option value="">선택하세요</option> 
<? 
while($row = mysql_fetch_array($result)){ 
?> 
<option value="<?=$row[make]?>"><?=$row[make]?></option> 
<? 
?> 
</select> 
<? 


밑에는 폼소스 입니다

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
   
function selectMobileOperator()
{
   var tel = $("select[name='tel']").val();
   var parameter = {
            "tel": tel,
            "mode": "select_tel"
         };
    
   $.ajax({
      url: "cu_input_ajax.php",
      type: "POST",
      data: parameter,
      dataType: "text",
      async: false,
      cache: false,
      success: function(data){
         $("select[name='make']").find("option").remove();
         $("select[name='make']").append(data);
      }
   });
}
</script>
 
<form>
<table class='type09'>
  <thead>
    <tr>
      <th colspan='4' scope="cols"></th>
    </tr>
  </thead>
  <tbody>
    <tr>
    <th>이동통신사</th>
            <td>
               <select name="tel" onchange="selectMobileOperator();">
                  <option value=''>선택하세요</option>
                                    <option value="SKT">SKT</option>
                                    <option value="KT">KT</option>
                                    <option value="LGT">LGT</option>
               </select>
            </td>
         </tr>
                  <tr>
            <th>제조사명</th>
            <td>
               <select name="make" onchange="selectCompany();">
                  <option value=''>선택하세요</option>
                                 </select>
            </td>
         </tr>
</table>
</form>

 

이 질문에 댓글 쓰기 :

답변 2

ajax로 넘어온 data( [] 괄호면 배열 )를 바로 select에 추가시켜서 생긴 문제네요.

 

아래 코드를 적당히 수정하셔서 사용하시면 되실꺼라 생각이 듭니다.

 


// find로 찾아서 remove 보단 그냥 지우는게 속도면에서 더 좋습니다.
// ​$("select[name='make']").find("option").remove(); 
​$("select[name='make']").html("");
​
for (i=0; i<data.length; i++) {
$("select[name='make']").append('<option value="'+data[i].value+'">'+data[i].value+'</option>'​);​
}
 

밑에는 폼소스 입니다 <---이줄 윗 부분이 cu_input_ajax.php 소스라는 말인가요?

 

success: function(data){ alert(data) 해보면 뭐가 문제인지 알 수 있을 겁니다

 

완전한 select문을 만들어서 넘겨주는데 append를 해서는 안됩니다

디비 추출 후 처리에서

<select name="make"> <--삭제

~~

</select> <---삭제

 

 

 

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
filter #ajax ×
전체 407
© SIRSOFT
현재 페이지 제일 처음으로