ajax를 이용하여 DB값 출력질문이요. 채택완료
범포신
10년 전
조회 8,163
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개
채택된 답변
+20 포인트
10년 전
ajax로 넘어온 data( [] 괄호면 배열 )를 바로 select에 추가시켜서 생긴 문제네요.
아래 코드를 적당히 수정하셔서 사용하시면 되실꺼라 생각이 듭니다.
</p><p style="font-size: 14.6666669845581px;">// find로 찾아서 remove 보단 그냥 지우는게 속도면에서 더 좋습니다.</p><p style="font-size: 14.6666669845581px;">// $("select[name='make']").find("option").remove();<span id="husky_bookmark_end_1437893525493"></span> </p><p style="font-size: 14.6666669845581px;"><span style="font-size: 14.6666669845581px; line-height: 1.5;"></span><span style="font-size: 14.6666669845581px; line-height: 1.5;">$("select[name='make']").html("");</span></p><p style="font-size: 14.6666669845581px;"><span style="font-size: 14.6666669845581px; line-height: 1.5;"></span></p><p style="font-size: 14.6666669845581px;">for (i=0; i<data.length; i++) {</p><p style="font-size: 14.6666669845581px; margin-left: 40px;"><span style="font-size: 14.6666669845581px; line-height: 1.5;">$("select[name='make']").append(</span><span style="font-size: 14.6666669845581px; line-height: 1.5;">'<option value="'+</span><span style="font-size: 14.6666669845581px; line-height: 1.5;">data[i].value</span><span style="font-size: 11pt; line-height: 1.5;">+</span><span style="font-size: 11pt; line-height: 1.5;">'</span><span id="husky_bookmark_end_1437892866474" style="font-size: 11pt; line-height: 1.5;"></span><span style="font-size: 11pt; line-height: 1.5;">">'+data[i].value+'</option>'</span><span style="font-size: 11pt; line-height: 1.5;"></span><span style="font-size: 11pt; line-height: 1.5;">);</span><span style="font-size: 11pt; line-height: 1.5;"></span></p><p style="font-size: 14.6666669845581px;">}</p><p><span style="font-size: 14.6666669845581px; line-height: 1.5;">
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인