简体   繁体   English

如何在codeigniter中的控制器索引中调用javascript函数?

[英]How to call javascript function in the index of a controller in codeigniter?

I'm trying to call a javascript function inside my controller to display a warning message in page if a verification I do in the index function of this controller is false. 如果我在此控制器的索引函数中进行的验证为错误,则尝试在控制器内部调用javascript函数以在页面中显示警告消息。

Here is my code: 这是我的代码:

<?php
public function index() {

    $this->load->model('uploads_m');
    $this->load->helper('form');
    $template_vars = Array();

    $this->load->vars($template_vars);

    $data = Array();
    $data['currentUploadId'] = $this->uploads_m->get_lastUploadId();
    $data['fileTypes'] = $this->uploads_m->getAllFileTypes();

    $data['existingFiles'] = Array();
    if (isset($data['currentUploadId'])) {
        $data['existingFiles'] = $this->uploads_m->get_UploadedFilesFromUploadId($data['currentUploadId']);
    }else {
        // TODO create warning message to tell that uploadid was not generated
    }
    $this->load->view('include/header');
    $this->load->view('upload_files', $data);
    $this->load->view('include/footer');
}
?>

I have a JS function stored in an extern js file that I wanted to call in this TODO. 我有一个JS函数存储在要在此TODO中调用的extern js文件中。 It should be called this way : 应该这样称呼:

show_msg_xajax("warning", "System was unable to find an Upload ID");

Since the check condition is being done in the index() of the controller, I don't know how to call this js function. 由于检查条件是在控制器的index()中完成的,所以我不知道如何调用此js函数。 if it was being invoked by an event in the view, I'd create an ajax method to execute this function. 如果它是由视图中的事件调用的,那么我将创建一个ajax方法来执行此功能。 but how can I call the javascript function it in the index() ? 但是如何在index()调用javascript函数呢?

I already checked this answer: Calling javascript function from controller codeigniter but it didn't help me. 我已经检查了这个答案: 从控制器codeigniter调用javascript函数,但并没有帮助我。

The solution I found was to send the function directly to the footer of that page... so I added a new variable to a footer template I have (where I call my javascripts). 我发现的解决方案是将函数直接发送到该页面的页脚...,因此我向我拥有的页脚模板(在这里称为javascript)添加了一个新变量。 in the index function in the controller I did: 在控制器的索引函数中,我做了:

if (isset($data['currentUploadId'])) {
        $data['existingFiles'] = $this->uploads_m->get_UploadedFilesFromUploadId($data['currentUploadId']);
} else {
        // TODO criar alerta de erro no sistema que não gerou UPLOADID
        $template_vars['foot_javascripts_inline'] = 'show_msg_xajax("error", "System was unable to find an Upload ID");';
}

and in my footer template I added: 在我的页脚模板中添加:

if (isset($foot_javascripts_inline)) { ?>
    <script> <?php echo $foot_javascripts_inline ?> </script>
}

Thanks anyway for the help 无论如何,谢谢您的帮助

您需要使用JS函数将文件添加为视图:

$this->load->view('path-to-js-message');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM