select 박스 선택이 되지 않음

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
select 박스 선택이 되지 않음

QA

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 해주시면 됩니다

전체 소스나 아니면 적용되어있는 주소가 있나요 ?

주소를 적어주시면 들어가서 보는게 좀더 바른 답변을 드릴수있습니다


<script>
function calculateStudent(e){

                $.ajax({
                    url : " http://localhost/bbs/dataloader/data_calculate.php",
                    type : "post",
                    data : {
                        studentName : $('#studentName').val()
                    },
                    success : function(res) {
                        $("#"+e.id+"").html(res);
                        console.log(res)
                    },
                    error : function(err){
                        alert(err);
                    }                                
                });                    
                }
</script>

<select id="studentName" onclick="calculateStudent(this)" class="frm_input">


#data_calculate.php

<?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[$i]?>" <?php echo ($write['studentName'] == $selectoption) ? " selected" : "";?>><?php echo $selectoption[$i]?></option>
    <?php
  }




<script>
function calculateStudent(e){

                $.ajax({
                    url : " http://localhost/bbs/dataloader/data_calculate.php",
                    type : "post",
                    data : {
                        studentName : $('#studentName').val()
                    },
                    success : function(res) {
                        $("#"+e.id+"").html(res);
                        console.log(res)
                    },
                    error : function(err){
                        alert(err);
                    }                                
                });                    
                }
</script>

<select id="studentName" onclick="calculateStudent(this)" class="frm_input">


#data_calculate.php

<?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[$i]?>" <?php echo ($write['studentName'] == $selectoption) ? " selected" : "";?>><?php echo $selectoption[$i]?></option>
    <?php
  }






이런 틀을 가지고 동작하는 거에요 아직 적용되어 있는 주소가 없어서요!!

답변을 작성하시기 전에 로그인 해주세요.
전체 0
QA 내용 검색
  • 개별 목록 구성 제목 답변작성자조회작성일
  • 질문이 없습니다.

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT