셀렉트 문의입니다.
본문
셀렉트값 저장 후 저장된 내용이 나오지 않습니다.
(디비에는 저장이 되어있습니다.)
설명을 드리자면 각 소속 그룹을 나누고 나누어진 소속 그룹의 부서나 있고, 직위/직급이 있습니다.
Group A - A Part1, A Part2 - A파트장, A팀장
Group B - B Part1, B Part2 - B파트장, B팀장
이렇게 구성되어 있을 때 Group B의 B Part1, B Part2 - B파트장, B팀장를 저장하고 보면 내용이 출력이 되어 있지 않습니다.
(A Part1, A Part2의 값으로 출력되어 내용이 없는 것으로 나옵니다.)
Group A의 A Part1, A Part2는 잘 출력됩니다.
<소스>
<select name="mb_1" id="mb_1" required class="required frm_input">
<option value="소속" <?php echo ($mb['mb_1'] == "소속") ? "selected" : "";?>>소속</option>
<?php
$mb_1 = explode('|','Group1|Group2');
$count = count($mb_1);
for($i=0; $i<$count; $i++){ ?>
<option value="<?php echo $mb_1[$i];?>" <?php echo ($mb['mb_1'] == "$mb_1[$i]") ? "selected" : "";?>><?php echo $mb_1[$i];?></option>
<?php } ?>
</select>
<? if($w==''){?>
<select name="mb_2" id="mb_2" required class="required frm_input">
<option value="부서" <?php echo ($mb['mb_2'] == "부서") ? "selected" : "";?>>부서</option>
</select>
<select name="mb_3" id="mb_3" required class="required frm_input">
<option value="직위/직급" <?php echo ($mb['mb_3'] == "직위/직급") ? "selected" : "";?>>직위/직급</option>
</select>
<? }else{ ?>
<? if($mb_1[0]){?>
<select name="mb_2" id="mb_2" required class="required frm_input">
<option value="부서" <?php echo ($mb['mb_2'] == "부서") ? "selected" : "";?>>부서</option>
<?php
$mb_2 = explode('|','A Part1|A Part2');
$count = count($mb_2);
for($i=0; $i<$count; $i++){ ?>
<option value="<?php echo $mb_2[$i];?>" <?php echo ($mb['mb_2'] == "$mb_2[$i]") ? "selected" : "";?>><?php echo $mb_2[$i];?></option>
<?php } ?>
</select>
<select name="mb_3" id="mb_3" required class="required frm_input">
<option value="직위/직급" <?php echo ($mb['mb_3'] == "직위/직급") ? "selected" : "";?>>직위/직급</option>
<?php
$mb_3 = explode('|','A파트장|A팀장');
$count = count($mb_3);
for($i=0; $i<$count; $i++){ ?>
<option value="<?php echo $mb_3[$i];?>" <?php echo ($mb['mb_3'] == "$mb_3[$i]") ? "selected" : "";?>><?php echo $mb_3[$i];?></option>
<?php } ?>
</select>
<? }else{ ?>
<select name="mb_2" id="mb_2" required class="required frm_input">
<option value="부서" <?php echo ($mb['mb_2'] == "부서") ? "selected" : "";?>>부서</option>
<?php
$mb_2 = explode('|','B Part1|B Part2');
$count = count($mb_2);
for($i=0; $i<$count; $i++){ ?>
<option value="<?php echo $mb_2[$i];?>" <?php echo ($mb['mb_2'] == "$mb_2[$i]") ? "selected" : "";?>><?php echo $mb_2[$i];?></option>
<?php } ?>
</select>
<select name="mb_3" id="mb_3" required class="required frm_input">
<option value="직위/직급" <?php echo ($mb['mb_3'] == "직위/직급") ? "selected" : "";?>>직위/직급</option>
<?php
$mb_3 = explode('|','B파트장|B팀장');
$count = count($mb_3);
for($i=0; $i<$count; $i++){ ?>
<option value="<?php echo $mb_3[$i];?>" <?php echo ($mb['mb_3'] == "$mb_3[$i]") ? "selected" : "";?>><?php echo $mb_3[$i];?></option>
<?php } ?>
</select>
<?php } ?>
<?php } ?>
<스크립트>
<script language="JavaScript">
$("#mb_1").change(function () {
var mb_1 = $(this).val();
switch (mb_1) {
case "<?php echo $mb_1[0];?>":
$('#mb_2').find('option').remove();
$('#mb_2').append("<option value='부서' <?php echo ($mb['mb_2'] == '부서') ? 'selected' : '';?>>부서</option>");
$('#mb_2').append("<?php $mb_2 = explode('|','A Part1|A Part2'); $count = count($mb_2); for($i=0; $i<$count; $i++){ ?> ");
$('#mb_2').append("<option value='<?php echo $mb_2[$i];?>' <?php echo ($mb['mb_2'] == '$mb_2[$i]') ? 'selected' : '';?>><?php echo $mb_2[$i];?></option>");
$('#mb_2').append("<?php } ?>");
$('#mb_3').find('option').remove();
$('#mb_3').append("<option value='직위/직급' <?php echo ($mb['mb_3'] == '직위/직급') ? 'selected' : '';?>>직위/직급</option>");
$('#mb_3').append("<?php $mb_3 = explode('|','A파트장|A팀장'); $count = count($mb_3); for($i=0; $i<$count; $i++){ ?> ");
$('#mb_3').append("<option value='<?php echo $mb_3[$i];?>' <?php echo ($mb['mb_3'] == '$mb_3[$i]') ? 'selected' : '';?>><?php echo $mb_3[$i];?></option>");
$('#mb_3').append("<?php } ?>");
break;
default :
$('#mb_2').find('option').remove();
$('#mb_2').append("<option value='부서' <?php echo ($mb['mb_2'] == '부서') ? 'selected' : '';?>>부서</option>");
$('#mb_2').append("<?php $mb_2 = explode('|','B Part1|B Part2'); $count = count($mb_2); for($i=0; $i<$count; $i++){ ?> ");
$('#mb_2').append("<option value='<?php echo $mb_2[$i];?>' <?php echo ($mb['mb_2'] == '$mb_2[$i]') ? 'selected' : '';?>><?php echo $mb_2[$i];?></option>");
$('#mb_2').append("<?php } ?>");
$('#mb_3').find('option').remove();
$('#mb_3').append("<option value='직위/직급' <?php echo ($mb['mb_3'] == '직위/직급') ? 'selected' : '';?>>직위/직급</option>");
$('#mb_3').append("<?php $mb_3 = explode('|','B파트장|B팀장'); $count = count($mb_3); for($i=0; $i<$count; $i++){ ?> ");
$('#mb_3').append("<option value='<?php echo $mb_3[$i];?>' <?php echo ($mb['mb_3'] == '$mb_3[$i]') ? 'selected' : '';?>><?php echo $mb_3[$i];?></option>");
$('#mb_3').append("<?php } ?>");
break;
}
});
</script>
무엇이 문제일까요??? 고수님들이 도움이 필요합니다..ㅠㅠ
답변 1
<?php
$mb_1_options = ['Group1', 'Group2'];
$mb_2_options = [
'Group1' => ['A Part1', 'A Part2'],
'Group2' => ['B Part1', 'B Part2']
];
$mb_3_options = [
'Group1' => ['A파트장', 'A팀장'],
'Group2' => ['B파트장', 'B팀장']
];
$options_json = json_encode([
'mb_2' => $mb_2_options,
'mb_3' => $mb_3_options
]);
?>
<select name="mb_1" id="mb_1" required class="required frm_input">
<option value="">소속</option>
<?php foreach ($mb_1_options as $option) { ?>
<option value="<?php echo $option; ?>" <?php echo ($mb['mb_1'] == $option) ? "selected" : ""; ?>><?php echo $option; ?></option>
<?php } ?>
</select>
<select name="mb_2" id="mb_2" required class="required frm_input">
<option value="">부서</option>
</select>
<select name="mb_3" id="mb_3" required class="required frm_input">
<option value="">직위/직급</option>
</select>
<script>
$(document).ready(function() {
var options = <?php echo $options_json; ?>;
function updateSelects() {
var selectedGroup = $('#mb_1').val();
// mb_2 업데이트
var $mb_2 = $('#mb_2').empty().append('<option value="">부서</option>');
if (selectedGroup && options.mb_2[selectedGroup]) {
$.each(options.mb_2[selectedGroup], function(i, option) {
$mb_2.append($('<option>', {
value: option,
text: option,
selected: option === '<?php echo $mb['mb_2']; ?>'
}));
});
}
// mb_3 업데이트
var $mb_3 = $('#mb_3').empty().append('<option value="">직위/직급</option>');
if (selectedGroup && options.mb_3[selectedGroup]) {
$.each(options.mb_3[selectedGroup], function(i, option) {
$mb_3.append($('<option>', {
value: option,
text: option,
selected: option === '<?php echo $mb['mb_3']; ?>'
}));
});
}
}
$('#mb_1').change(updateSelects);
// 페이지 로드 시 초기 상태 설정
updateSelects();
});
</script>