Link to home
Start Free TrialLog in
Avatar of justmelat
justmelat

asked on

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
} 

Open in new window


and here is the button:
<input type="submit" id="btn_Add" name="btn_Add" value="Submit"> 

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I may not be understanding this correctly, but it looks like you've decoupled the front end from the server-side action script, yet you want to depend on the success of the server-side action script.  I think that is a computer-science no-no.  Can you please explain in plain language what effect you are trying to achieve and why?  Maybe there is a predictable design pattern that can meet your needs.
Avatar of justmelat
justmelat

ASKER

Hi Ray:

When I hit the submit button, I want to disable it immediately, change it's display value to Sending...[just a visual for the client] then I want the button to continue doing what it was set to do which was to call the controller and run a bunch of functions to send the data on.  

Currently, you hit submit, controller is called, all functions run - but the problem is, depending on the issues with the client's browser and/or network, the user "may" be able to hit the submit button again and again be of sluggishness.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Ray:

thanks for the idea:

this seems to be working, I just hid the submit button upon click using jquery: $(document).ready(function(){ $("#btnSubmitJob").click(function(){ $(this).hide(); $("#btnSubmitJob").before("Processing...."); }); }); –
You might want to do both - hide the button and also user server-side programming to avoid re-processing the data.  I am wondering what might happen if the client fiddled with the back and forward browser controls?