ajax 문의드립니다.
전에 문의했던 것 다 해결했는데
뷰
$.ajax({
type:"POST",
data : {"id" : $("#id").val()},
url:"http://127.0.0.1/index.php/blog/a_ajax",
dataType:"json",
success:function(data) {
alert(result[0].id);
$("#Select").php("<option value=''>선택</option>");
for( var i=0 ; i < json.length ; i++){
$("#Select").append("<option value='"+json[i].id+"' >"+json[i].name+"</option>");
}
}
});
컨트롤러
function a_ajax(){
$data = $this->input->post('id');
$result = $this->Ad_m->ajaxA($data);
-----------------------
$this->output
->set_content_type('application/json')
->set_output(json_encode($result));
-----------------------
}
모델
function ajaxA($data)
{
$sql = "select id, name from admin where id='$data' order by name";
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
return $query->result_array();
}
}
}
배열로 컨트롤러에 받아진 것까지는 되는데
-------------------
내용
-------------------
컨트롤러
이 부분이 어렵습니다. 그냥 echo json_encode($result);로 하든 뭘하든간에
파이어폭스로 봤을때 뷰에서 표현하는 방법을 모르겠습니다.
짧은 시간에 제이쿼리부터 다 보려니 어렵네요. 이것까지 되면 파일업다운 해보고
그다음부턴 대부분 처리될거같은데 삼일째 삽질하네요...
리맵은 웅파님의
public function _remap($method)
{
if (!strpos($method, '_ajax'))
{
이걸 쓰고 있습니다.
근데 헤더 푸터는 표현되는데 어떡게 해야할지 감이 안잡히네요...
그런데 $.ajax의 success의 코드를 보면 여러 명의 admin을 받는 것같네요. 여러명을 받는 것이 맞다면 먼저 모델 함수를 수정해야 합니다.
[code]
function ajaxA($data)
{
$sql = "select id, name from admin where id='$data' order by name";
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
$list = array();
while ($query->result_array() as $row) {
$list[] = $row;
}
return $list;
}
return array();
}
[/code]
그리고 $.ajax 함수도 다음처럼 수정하면 됩니다:
[code]
$.ajax({
type:"POST",
data : {"id" : $("#id").val()},
url:"http://127.0.0.1/index.php/blog/a_ajax",
dataType:"json",
success:function(data) {
var result = $.parseJSON(data);
//alert(result[0].id);
$("#Select").html("<option value=''>선택</option>");
for( var i=0 ; i < result.length ; i++){
$("#Select").append("<option value='"+result[i].id+"' >"+result[i].name+"</option>");
}
}
});
[/code]
$("#Select").php에서 php는 html을 말하는 거겠죠?