user1176783
user1176783

Reputation: 673

How do I call MVC Controller from with Javascript function

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

Upvotes: 0

Views: 2456

Answers (2)

Philip
Philip

Reputation: 4592

index.php

<script>
    // create a global var before calling your external
    // javascript file(s).
    var BASE_PATH = "<?php echo base_url();?>";
</script>
<script src="link_to_myjavascript.js"></script>

myjavascript.js (jQuery example)

(function($){

    $(function(){

          var do_ajax = function(some_params){
               $.ajax({
                    url : BASE_PATH + 'controller/method',
               });
          }

          if(conditions)
          {
             do_ajax(some_params);
          }

    });

})(jQuery);

Upvotes: 1

Churk
Churk

Reputation: 4627

Look at ajax call. Using prototypejs or Jquery

<input type="button" onclick="dosomething()" />

example

<script>
function dosomething() {
  var url = "something.php";
  new Ajax.Request(url, {
    parameters: {//parameters},
    onSuccess: function(transport){
      // do something when response is good
    },
    onFailure: function (request) {
      // Do something when somehting goes wrong     
    });
}
</script>

Upvotes: 0

Related Questions