Would like to know the ways to update the session variable in JavaScript function when the submit button is clicked and the same variable to be accessed in controller class.
The below links provides information on setting session variable in function
Pass Javascript Value to Java in JSP
and
JSP - how to pass a javascript var in session.setAttribute?
But, I need to update session variable only when the checkbox is selected and pass to java controller on form submission.
The session variable in javascript function can be updated only if you update the variable on the server. But since the javascript is executing in the browser you can't update the session variable.
Then you need update the page where this session variable is used.
If you can't update the part of the page and need to update the whole page to make changes effective then send redirect result with the response that refreshes the page. It will reinitialize the javascript with the variable in session.
The first part of your question , as i understand you are trying to update the session from javascript , you can do an AJAX call to the action form and update the session variable as you usually do in Struts 2 , the page won't refresh .
Related
If I have a form on a page like so - (note the model property):
<form asp-action="ProcessQuote" id="form-calc-total">
<p id="calc-item">#Model.TheCounter.Value</p>
<button id="btn-add">+</button>
<button id="btnContinue" onclick="document.getElementById('form-calc-total').submit();">CONTINUE</button>
</form>
Let's say I have an event listener on the form so when btn-add is clicked, it triggers the following JS function which adds 1 to the counter.
function calculatorClick() {
let cou = document.querySelector('#calc-item');
cou.innerHTML = Number(cnt) + 1;
}
So now when the form submits, it hits the controller and within that I can store the number (#Model.TheCounter.Value) in a session variable if I want:
HttpContext.Session.SetString(SESSION_GUID, model.TheCounter);
That means if I revisit the page I can pull out the session variable and populate that field again.
But that's only stored on form submission - How can I get a session variable to store after each button click so it'd remember the counter without someone having to submit the form first?
I vaguely remember in PHP that there were SESSION variables that could be added to directly on the page with JS (I think) - but how can I do this within an ASP.NET CORE framework / MVC pattern?
Is this something AJAX would be used for?
But that's only stored on form submission - How can I get a session
variable to store after each button click so it'd remember the counter
without someone having to submit the form first?
The session value was stored on the server side, if you want to update and get the session value, you could create an Action method, then use JQuery Ajax to call this method and then update the session and get the latest value.
More detail information about using JQuery Ajax with Asp.net Core, see the following links:
jQuery AJAX and JSON Example in ASP.Net Core MVC
How to use jQuery AJAX method to call an Action method in ASP.NET Core
Besides, on the client side, you could also use web storage API to store the data, check the following tutorials:
Using the Web Storage API
HTML Web Storage API
How to get latest value of a session without refreshing the page?
I want to set a variable with dynamic values depending upon which button is clicked.I am using the session variable for this purpose but am not getting latest values.I have to refresh the page to update the session.I am using session because I want the variable to be in global scope.
You can use the setInterval function on document.ready event along with AJAX to fetch and update the Session periodically through PHP.
A possible solution how to use the above is well depicted in the below url .
setInterval and Ajax
Hope this helps .
I am using a PHP variable to load a part of the view,
here is a part of the code
<div id="container">
<?php echo $view ?> //$view has view loaded in it
</div>
on a button press I update the innerHTML of the continer through ajax call.
But the problem is that, when I navigate on my site (through forward and backward arrows), and come back to this view, the old value of PHP variable is loaded again, and ajax updates disappear.
What is the best way of updating this PHP variable?
I'd say by setting a session or cookie to save the ajax call on the serverside. Then in your controller check for the session, and if it doesn't exist show the initial view.
On your ajax call, on server-side, you are returning the new value. You should also update the php variable source (a session variable, database, etc). This way, when the page is loaded again, the variable $view will have the proper value.
and using JavaScript Validation,
I am unable to submit my form because JavaScript session variable is getting one step previous value,
while onother hand user is Seeing new Captcha value,
how can I get rid of this,
I am using
<script>
var voo = ("<?php echo $_SESSION['captcha'];?>");
alert(voo);
</script>
I am working online,
Here is the live demo that i am facing,
http://imperialsoft.com.pk/dwriters/submit-your-work/
Javascript is clientside and session is on the server. So,
set your session variable value in server side, and then use that session variable to retrieve the value in javascript.
or
You could write some javascript on the server-side and put the session variable in it and the render the result to your page.
Hope this helps,
Thanks.
Setting global variables in my js file doesn't seem to work. How can I set rails session variables from JavaScript?
I'd like a solution that doesn't use jQuery.
Edit: The solution is to use an HTML form containing hidden fields.
Global variables only live for the duration of a page view. Once the page is rerendered, they are reset to what their default values are.
If you need to keep the values, you need to use cookies or local storage. Other option is to submit them to the server with Ajax and have the server remember them and set the values when the page is rendered.