ajax 페이징 관련 질문드립니다^^
본문
form 값을 입력받아서 검색결과를 출력하는 페이지를 만들고 있습니다.
form 값중에 input =checkbox가 있는데.
<input type="checkbox" name="sub_smode[]"<?php if(is_array($_GET['sub_smode']) && in_array("company", $_GET['sub_smode'])) echo " checked" ?> value="company" id="sub_smode_company" /><label for="sub_smode_company">기업명</label>
<input type="checkbox" name="sub_smode[]"<?php if(is_array($_GET['sub_smode']) && in_array("subject", $_GET['sub_smode'])) echo " checked" ?> value="subject" id="sub_smode_subject" /><label for="sub_smode_subject">제목</label>
<input type="checkbox" name="sub_smode[]"<?php if(is_array($_GET['sub_smode']) && in_array("content", $_GET['sub_smode'])) echo " checked" ?> value="content" id="sub_smode_content" /><label for="sub_smode_content">내용</label>
이렇게 name을 배열값으로 받아 넘기고 있습니다.
그런데 ajax 페이징을 할때 문제가 발생합니다.
3개 모두 체크를 하고 검색을 진행하면
첫 페이지는 sub_smode[]=content&sub_smode[]=subject&sub_smode[]=company
이렇게 잘 받아지는데,
$.get("./search_ajax.php", {
ajax: 1, page : Number(list.attr("data-page")) + 1,
foreach($_GET as $array => $value ){
if($array=="page") {continue;}
if($array=="sub_smode"){
echo " sub_smode[] : \" ";
foreach($value as $sub_mode => $sub_value){
echo " '{$sub_value}' ,";
}
echo " \" ";
continue;
}
echo " {$array} : '{$value}' ,";
}
?>
이렇게 값을 받아 넘기면
Uncaught SyntaxError: Unexpected token [ 이런 에러가 발생하고
그냥
$.get("./search_ajax.php", {
ajax: 1, page : Number(list.attr("data-page")) + 1,
<?php
foreach($_GET as $array => $value ){
if($array=="page") {continue;}
if($array=="sub_smode"){
foreach($value as $sub_mode => $sub_value){
echo " $array : '{$sub_value}' ,";
}
continue;
}
echo " {$array} : '{$value}' ,";
}
이렇게 넘기면 체크된 값중 맨 나중 값만 넘어갑니다.
저는 체크된 값을 모두 넘겨서 다음페이징을 받아올때도 같은 조건으로 결과값을 받아오고 싶은데
어떻게 해결해야 잘 처리 될까요?ㅠ
답변 2
이틀동안 고민하다가 질문을 올렸던건데.ㅠㅠㅠㅠ
항상 질문을 올리고 나면 해결이 되네요ㅠㅠㅠ
자문자답입니다.!!
<script>
var checkboxValues=['<?php echo $_GET['sub_smode'][0]; ?>','<?php echo $_GET['sub_smode'][1]; ?>','<?php echo $_GET['sub_smode'][2]; ?>' ];
$.get("./search_ajax.php", {
ajax: 1, page : Number(list.attr("data-page")) + 1,
<?php
foreach($_GET as $array => $value ){
if($array=="page") {continue;}
if($array=="sub_smode"){
continue;
}
echo " {$array} : '{$value}' ,";
}
?>
sub_smode : checkboxValues
},
이렇게 해결하였습니다^^
var checkboxValues= <?php
if($_GET['sub_smode']){
echo "[";
if($_GET['sub_smode'][0]){
echo "'".$_GET['sub_smode'][0]."'";
}
if($_GET['sub_smode'][1]){
echo ",'".$_GET['sub_smode'][1]."'";
}
if($_GET['sub_smode'][2]){
echo ",'".$_GET['sub_smode'][2]."'";
}
echo "]";
}
?>
로 수정