正确地将数据从 Controller 传递到 View CodeIgniter
Correctly Passing data from Controller to View CodeIgniter
这个问题可能会被问很多次,但我很难回答。
var_dump($lex_post_data);在控制器上工作正常
我的控制器代码
try{
// need for the manage functionality to be initialized.
$manage_mode = FALSE;
$appointment = array();
$provider = array();
$customer = array();
$lex_post_data = $this->input->post('lexname');
var_dump($lex_post_data);
// Load the book appointment view.
$view = array (
'available_services' => $available_services,
'available_providers' => $available_providers,
'company_name' => $company_name,
'manage_mode' => $manage_mode,
'appointment_data' => $appointment,
'provider_data' => $provider,
'customer_data' => $customer,
'post_data' => $lex_post_data
);
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
}
$this->load->view('appointments/book', $view);
查看代码:
<script type="text/javascript">
var GlobalVariables = {
availableServices : <?php echo json_encode($available_services); ?>,
availableProviders : <?php echo json_encode($available_providers); ?>,
baseUrl : <?php echo '"' . $this->config->item('base_url') . '"'; ?>,
manageMode : <?php echo ($manage_mode) ? 'true' : 'false'; ?>,
appointmentData : <?php echo json_encode($appointment_data); ?>,
providerData : <?php echo json_encode($provider_data); ?>,
customerData : <?php echo json_encode($customer_data); ?>,
lexpostData : <?php echo json_encode($lex_post_data); ?>,
csrfToken : <?php echo json_encode($this->security->get_csrf_hash()); ?>
};
console.log(GlobalVariables);
var EALang = <?php echo json_encode($this->lang->language); ?>;
var availableLanguages = <?php echo json_encode($this->config->item('available_languages')); ?>;
$(document).ready(function() {
FrontendBook.initialize(true, GlobalVariables.manageMode);
// GeneralFunctions.centerElementOnPage($('#book-appointment-wizard'));
GeneralFunctions.enableLanguageSelection($('#select-language'));
});
</script>
问题 1:这是访问从控制器发送的值的正确方法吗?
问题 2:关于 console.log(GlobalVariables);我得到
lexpostData : null
我做错了什么。
请指导
编辑
已解决并已关闭:我试图在视图中获取不同的名称变量。必须使用
lexpostData : <?php echo json_encode($post_data); ?>,
而不是
lexpostData : <?php echo json_encode($lex_post_data); ?>,
在控制器中,您在 post_data
中传递 $lex_post_data
'post_data' => $lex_post_data
所以在视图中而不是
<?php echo json_encode($lex_post_data); ?>
使用
<?php echo json_encode($post_data); ?>
这个问题可能会被问很多次,但我很难回答。 var_dump($lex_post_data);在控制器上工作正常
我的控制器代码
try{
// need for the manage functionality to be initialized.
$manage_mode = FALSE;
$appointment = array();
$provider = array();
$customer = array();
$lex_post_data = $this->input->post('lexname');
var_dump($lex_post_data);
// Load the book appointment view.
$view = array (
'available_services' => $available_services,
'available_providers' => $available_providers,
'company_name' => $company_name,
'manage_mode' => $manage_mode,
'appointment_data' => $appointment,
'provider_data' => $provider,
'customer_data' => $customer,
'post_data' => $lex_post_data
);
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
}
$this->load->view('appointments/book', $view);
查看代码:
<script type="text/javascript">
var GlobalVariables = {
availableServices : <?php echo json_encode($available_services); ?>,
availableProviders : <?php echo json_encode($available_providers); ?>,
baseUrl : <?php echo '"' . $this->config->item('base_url') . '"'; ?>,
manageMode : <?php echo ($manage_mode) ? 'true' : 'false'; ?>,
appointmentData : <?php echo json_encode($appointment_data); ?>,
providerData : <?php echo json_encode($provider_data); ?>,
customerData : <?php echo json_encode($customer_data); ?>,
lexpostData : <?php echo json_encode($lex_post_data); ?>,
csrfToken : <?php echo json_encode($this->security->get_csrf_hash()); ?>
};
console.log(GlobalVariables);
var EALang = <?php echo json_encode($this->lang->language); ?>;
var availableLanguages = <?php echo json_encode($this->config->item('available_languages')); ?>;
$(document).ready(function() {
FrontendBook.initialize(true, GlobalVariables.manageMode);
// GeneralFunctions.centerElementOnPage($('#book-appointment-wizard'));
GeneralFunctions.enableLanguageSelection($('#select-language'));
});
</script>
问题 1:这是访问从控制器发送的值的正确方法吗? 问题 2:关于 console.log(GlobalVariables);我得到
lexpostData : null
我做错了什么。
请指导
编辑
已解决并已关闭:我试图在视图中获取不同的名称变量。必须使用
lexpostData : <?php echo json_encode($post_data); ?>,
而不是
lexpostData : <?php echo json_encode($lex_post_data); ?>,
在控制器中,您在 post_data
$lex_post_data
'post_data' => $lex_post_data
所以在视图中而不是
<?php echo json_encode($lex_post_data); ?>
使用
<?php echo json_encode($post_data); ?>