簡體   English   中英

如何在codeigniter中的控制器索引中調用javascript函數?

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

如果我在此控制器的索引函數中進行的驗證為錯誤,則嘗試在控制器內部調用javascript函數以在頁面中顯示警告消息。

這是我的代碼:

<?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');
}
?>

我有一個JS函數存儲在要在此TODO中調用的extern js文件中。 應該這樣稱呼:

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

由於檢查條件是在控制器的index()中完成的,所以我不知道如何調用此js函數。 如果它是由視圖中的事件調用的,那么我將創建一個ajax方法來執行此功能。 但是如何在index()調用javascript函數呢?

我已經檢查了這個答案: 從控制器codeigniter調用javascript函數,但並沒有幫助我。

我發現的解決方案是將函數直接發送到該頁面的頁腳...,因此我向我擁有的頁腳模板(在這里稱為javascript)添加了一個新變量。 在控制器的索引函數中,我做了:

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");';
}

在我的頁腳模板中添加:

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

無論如何,謝謝您的幫助

您需要使用JS函數將文件添加為視圖:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM