【问题标题】:How to call javascript function in the index of a controller in codeigniter?如何在codeigniter的控制器索引中调用javascript函数?
【发布时间】:2013-12-13 20:03:35
【问题描述】:

如果我在此控制器的索引函数中进行的验证为假,我正在尝试在控制器中调用 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 文件中的 JS 函数,我想在这个 TODO 中调用它。 应该这样称呼它:

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

由于检查条件是在控制器的index()做的,所以不知道怎么调用这个js函数。 如果它被视图中的事件调用,我会创建一个 ajax 方法来执行这个函数。但是如何在index() 中调用javascript 函数呢?

我已经检查了这个答案:Calling javascript function from controller codeigniter,但这对我没有帮助。

【问题讨论】:

  • 不知道你在问什么。 javascript 在客户端浏览器中运行,而不是在 php 中。您需要在两者之间建立更好的联系
  • 检查我的答案..你可能会明白..对不起,如果我不清楚..
  • 你为什么不在视图上这样做

标签: javascript php codeigniter


【解决方案1】:

您需要将带有 JS 功能的文件添加为视图:

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

【讨论】:

  • 我找到了解决方案..我会发布它作为答案
【解决方案2】:

我找到的解决方案是将函数直接发送到该页面的页脚......所以我在我拥有的页脚模板中添加了一个新变量(我称之为我的 javascripts)。 在我所做的控制器的索引函数中:

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>
}

谢谢你的帮助

【讨论】:

  • 但是你说需要在extern JS文件中调用JS函数。您的答案就是将数据发送到您的视图。您可以通过多种方式做到这一点..
  • 但是 show_msg_xajax() 函数在外部 js 文件中...我不是在尝试调用 js 文件。我想在外部 js 文件中调用 js 函数(标题的问题是这样说的......):),但还是谢谢!
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 2017-07-22
相关资源
最近更新 更多