select 박스 선택이 되지 않음
본문
안녕하세요!! 궁금한게 있어서 질문드릴려고 합니다!!
제가 ajax로 데이터를 보내서 쿼리문에 입력을 하고
$sql = " select '".$studentName."%' from student_class " ;
$result = sql_query($sql);
for($i=0; $row=sql_fetch_array($result); $i++){
$selectoption[] = $row['STUDENT_NAME'];
?>
<option><?php echo $selectoption[$i]?></option>
<?php
}
이런식으로 값을 불러와서 select 박스에 옵션을 넣으려고 하는데 값은 select 박스 안으로 들어오는데 옵션들을 선택해도 계속 맨 위의 값만 선택이 되고 바뀌질 않습니다!!ㅠㅠㅠㅠ
도대체 무엇이 문제인지 몰라서 이렇게 질문글 남깁니다. 고수님들 도와주세요!!
!-->답변 3
소스를 적용해서 해보려고 하는데
코드가 좀 이상하네요
우선 하실려는 처리가
. 학생명 입력시 해당 학생명 을 포함한 결과를 가져와서 select 로 뿌려주려는 것이 맞나요 ?
위 내용이 맞다면
. 학생이름을 넣는 입력칸이 별도로 있어야 하고 selectbox 도 별도로 있어야합니다
. 지금 코드는 selectbox 의 값을 가져와서 그걸 그대로 ajax 처리해서 option 출력하는 건데
이건 의미가 없을꺼 같고요
<script>
function calculateStudent(){
$.ajax({
url : "./data_calculate.php",
type : "post",
data : {
studentName : $('#studentName').val()
},
success : function(res) {
$("#studentselect").html(res);
},
error : function(err){
alert(err);
}
});
}
</script>
<input type="text" id="studentName" name="studentName" ><button value="검색" onclick="calculateStudent();"> 검색</button>
<select id="studentselect" name="studentselect" class="frm_input">
</select>
이런식으로 하시고
<?php
include_once('./_common.php');
$studentName = $_POST['studentName'];
$sql = " select * from student_class where studentName LIKE '".$studentName."%' " ;
$result = sql_query($sql);
for($i=0; $row=sql_fetch_array($result); $i++){
$selectoption = trim($row['studentName']);
?>
<option value="<?php echo $selectoption?>" <?php echo ($studentName == $selectoption) ? " selected" : "";?>><?php echo $selectoption?></option>
<?php
}
?>
코드 쪽에선 위처럼 해주시면 됩니다
$sql = " select '".$studentName."%' from student_class " ;
$result = sql_query($sql);
for($i=0; $row=sql_fetch_array($result); $i++){
?>
<option value="고유값"><?php echo $row['STUDENT_NAME']?></option>
<?php
}
1. option value 안에 고유값을 넣어주세요
2. 반복문인데 [] <= 배열에 담았다 출력하실 필요없이 바로 echo 해주시면 됩니다
!-->전체 소스나 아니면 적용되어있는 주소가 있나요 ?
주소를 적어주시면 들어가서 보는게 좀더 바른 답변을 드릴수있습니다
답변을 작성하시기 전에 로그인 해주세요.