[SalesForce] way to call an Apex Controller from Lightning JavaScript Controller

I am studying it still, I am new here and have not much experience, what I am trying to do is:

  1. Trying to pass a date/time value from an input field through JavaScript to the Apex Controller; I've got this one working now, and

  2. now what's happening is, I want to call the Apex Controller so I can use the start and end dates in my query's where clause. Because the controller is being called when the app is opened, when I hit the button, my query is not being executed again, that is a way to solve that? Maybe by calling a new method in my JavaScript after the user presses the button?

Best Answer

Trying to pass the datetime (in the input) through the JS to the apex controller - This one is working now

I would suggest provide some more information about what have you done with the first requirement what kind of JS action invocation you did.

Update: (thanks to @sfdcfox for mentioning it Lightning Enabled tag question)

Using Lightning:

If you are looking that thing in Lightning perspective then I'm assuming you used @AuroEnabled Client-server enabled menthod to pass the Date into apex controller, Now use action.setCallback() to invoke another method and pass your parameters from JS directly via action.setParams().

see below reference on more info: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

see more info on action.setCallback(): Use of setCallBack() in Lightning

Using Visualforce (Conventional Way):

There are various ways to invoke any second method/action on completion of other, it depends on your first action invocation type. If you want to use JS for calling action then use <apex:actionFunction>, it supports rerender attribute which will helps you to rerender the result on the page side.

if you used <apex:actionSupport> or <apex:commandLink> or <apex:commandButton> or <apex:actionFunction> as your first invocation, then you can use onCompletion attribute of these action methods, and in that oncompletion you can call any other <apex:actionFunction> which eventually calls your controller action.

see more on <apex:actionFunction> : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm