Php not getting session latest value - javascript

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 .

Related

Updating session variable in JavaScript function in Struts 2

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 .

Using Global Variables between multiple functions in JQuery?

I want to dynamically load an image using jQuery like this:
main.js
var slidersrc=""; //try to define global variable - not sure if this is correct
jQuery(document).ready(function() {
jQuery("#sliderimg").attr('src', slidersrc);
});
jQuery("#selection1").click(function() {
slidersrc='wp-content/themes/*****/slide1.png';
});
So the first time user access my website, the slider is empty. After user clicks on one of the selection areas, I set the global variable value. Then if user continues to navigate at my website to different pages, the user should be shown a slider image as a result of his selection.
However, this doesn't appear to work.
Am I correctly using the global variable in jQuery? Or is there a better way to save the user selection value in client side?
thanks!
Global variables do NOT survive from one page to the next. Each page starts an entirely new javascript context (all new global variables, functions, etc...).
If you want to save state from one page to the next, your options are:
Put the data in a cookie which you can read from each successive page when that page loads.
Put the data in a browser local storage which you can read with javascript from each successive page when that page loads (recommended option).
Store the data on the server and embed it in each page as it is served from the server.
You can read about how to read and write from browser LocalStorage here and here.
If you're planning on changing the slider image each time the user clicks, then perhaps you want to save an index into an image array in local storage. When the page loads, you read the current index from localStorage (or supply a default value if no value exists in local storage), then write back the current value to localStorage for the next page. If the user takes some action that causes the index to update to a new value, then you update your page and then write that new index into localStorage so the next page can read it from there and so on.
LocalStorage is a similar concept to cookies, but it's a bit easier to manage and more efficient (the data is not sent to the server with every page request).

Best way of updating PHP variables used in views

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.

get Latest session value in Javascript

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.

Save a Javascript variable between user interactions?

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.

Categories