ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Call MVC Controller from with Javascript function

March 28, 2012 10:54am

Subscribe [3]
  • #1 / Mar 28, 2012 10:54am

    justmelat

    20 posts

    I have a onclick event on a submit button in my CI app.  So when user clicks submit, it goes to my js function that disables the button, but it does not continue processing.  I used this “document.forms[“mainFrm”].submit();”, but because of the way the code is written I need it to go directly to a controller and finish processing. 

    So how do I call a CI controller from my js function?

    Here is the function that is being called onClick:

    function disableWhenSubmit()
    {
     alert ("You did get here");
     var holdBtnElement = document.getElementById('btn_Add');
     holdBtnElement.disabled = true;
     holdBtnElement.value = "sending ...";
     //document.forms["createRequestForm"].submit();
     <?= base_url();?>index.php/request"; //this is what I am working on
    }

    and here is the button:

    <input type="submit" id="btn_Add" name="btn_Add" value="Submit">
  • #2 / Mar 28, 2012 11:12am

    Kamarg

    241 posts

    Most likely you want to look into doing an ajax request. Or if you don’t care if the page refreshes, you just ignore all the javascript and let the html form submit normally.

  • #3 / Mar 28, 2012 11:15am

    Matalina

    191 posts

    You can’t use php in an external javascript file unless your php parser parses .js files, or if you extranal script is a .php with a javascript header at the start, but you can’t call ci code inside of the external javascript file unless it’s processed through ci with a controller.

    I tend to do this for my straight js files:  Note: remove the index.php part if you are using .httaccess to remove it.

    $url = [removed].href.toString();
    re = /^(http:\/\/\w*\.*(\w+\.\w+)\/.*)\/*index\.php*/;
    matches = re.exec($url)
    
    var $base_url = matches[0];
    var $img_url = matches[1];

    You’ll have to use XMLHttpRequest as well to process said script.

    If your javascript is in the view then you can use php like you have it.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases