웹 사이트의 일부 내용을 동적으로로드하기 위해 ajax를 사용합니다. 뷰 자체에 데이터를 보낼 필요가 없으므로 레지스터 및 로그인 양식을로드 할 때 잘 작동합니다 (register_view
, 등).
하지만, 예를 들어 사용자의 프로필과 같이 다른 무언가를로드하려고하면 뷰에 일부 변수를 전달해야하며 여기에는 AJAX에 문제가 생길 때가 있습니다.
그리고 내가 보낸 변수가 isset
과 !empty
컨트롤러에서 확인되었습니다. 하지만보기에는 갑자기 변수가 정의되지 않게됩니다. 이것은 AJAX를 통해 프로필에 액세스 할 때만 발생합니다.
PHP 코드 :
컨트롤러 :정의되지 않은 변수, AJAX를 통해 뷰를로드 할 때
if($this->uri->segment(4)){//if viewing a specific profile.
/*escape the uri segment*/
$segment = intval($this->uri->segment(4));
if($segment == 0){//the uri segment was a string
/*display error message.*/
$data['content'] = 'redirect_message';
$data['information'] = 'Could\'nt find the profile!, please try again.';
$this->load->view('templates/manage', $data);
}
else{//else , the uri segment is a number, considered safer.
$query_result = $this->db_model->getProfile($segment);//get the Profile
/*check if any results were returned.*/
if($query_result->num_rows() > 0){
/*load a view to display the specified Profile.*/
$data['information'] = $query_result;
if($this->input->is_ajax_request())//requesting via ajax, display the content only.
$this->load->view("view_Profile_view", $query_result);
else{
$data['content'] = 'view_Profile_view';
$this->load->view('templates/manage', $data);
}
}
else{ //no rows returned.
/*show error message.*/
$data['content'] = 'redirect_message';
$data['information'] = 'Error viewing the Profile!';
$this->load->view('templates/manage',$data);
}
}
}
보기 (view_Profile_view
) :
/*display the profile:*/
$row = $information->row();//error occurs here!
echo $row->username.'</br>';
echo $row->email;
jQuery를/JS 코드 : 그것의 모습으로
var base_url = "/";
var site_url = "/index.php/";
$(document).ready(function(){
$('.ajax_anchor').click(function(){
loadForm(this);
return false;
});
});
function loadForm(anchor){
var splitted_url = $(anchor).attr('href').split("/");
if(splitted_url.length == 7){//probably accessing /site/login or /site/register not something like /site/profiles/view/[ID].
var url = splitted_url[splitted_url.length-2]+"/"+splitted_url[splitted_url.length-1];
}
else if(splitted_url.length == 9) {//probabbly accessing /site/profiles/view/[ID] not something like /site/login.
var url=
splitted_url[splitted_url.length-4]+"/"
+
splitted_url[splitted_url.length-3]+"/"
+
splitted_url[splitted_url.length-2]+"/"
+
splitted_url[splitted_url.length-1]+"/"
;
}
var csrf_token = $.cookie('csrf_cookie_name');//holding the csrf cookie generated by CodeIgniter, using jQuery cookie plugin
$.ajax({
type: "POST",
url: site_url+url,
data: {csrf_test_name:csrf_token}//pass the csrf token otherwise codeigniter will return an error(500).
}).done(function(html) {
var ajaxResult$ = $('#ajax_result');//ajax_result is an empty div, used to display ajax results.
ajaxResult$.empty().append(html).dialog();//dialog:is a jquery-ui function.
});
}
그래서 어디에 "정의되지 않은 변수"오류가 발생합니까? – Bergi
@Bergi Oh! 나는 지금보기를 잊어 버렸다. – Abdulaziz
'$ information'에 vardump를 시도하면 – xbonez