2

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.

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. 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. if it was being invoked by an event in the view, I'd create an ajax method to execute this function. but how can I call the javascript function it in the index()?

I already checked this answer: Calling javascript function from controller codeigniter but it didn't help me.

3
  • have no idea what you are asking. javascript runs in client browser, not in php. You need to make better connection in question between the two Commented Nov 27, 2013 at 23:36
  • check my answer.. u may then understand.. sorry if I wasn't clear.. Commented Nov 28, 2013 at 0:01
  • why dont you do it on view Commented Nov 28, 2013 at 6:32

2 Answers 2

2

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). 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

Sign up to request clarification or add additional context in comments.

2 Comments

But you said you need to call JS function in extern JS file. Your answer is nothing than sending data to your view. You can do it in so many ways..
but the show_msg_xajax() function is in a external js file... I wasn't trying to call a js file.. I wanted to call a js function inside an external js file (the title of the question says it...) :), but thanks anyway!
1

You need to add your file with JS function as a view:

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

1 Comment

I found a solution.. I'll post it as an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.