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.
Related
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 .
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'm using history.pushState() in my wordpress site to changes the url/title of the page once a new post is dynamically loaded (AJAX). I want to store each post's id in an array, but for some reason the array variable gets re-defined.
At the top of my page I define the array variable:
<?php $array = array(); ?>
So when I push data to the array it only pushes to index 0.
Some help would be greatly appreciated.
pushState changes only the history stack.
It doesn't change any other variable accessible to JavaScript.
It couldn't possibly change any PHP variable because it runs on the client and does not trigger any HTTP request.
PHP is server side and is evaluated once and for all on each load. Hence why it is 0.
As #Quentin says in his answer: history.pushState is JavaScript (front-end). It has no connection with PHP (back-end) whatsoever.
I have this code:
<img src="{$img_dir}/product_grid_view.png" class="pgrid" title="Grid View" onclick="document.getElementById('product_list').id = 'product_grid'; return false"></img>
<img src="{$img_dir}/product_list_view.png" class="plist" title="List View" onclick="document.getElementById('product_grid').id = 'product_list'; return false"></img>
The following code works well and does its job but the main problem is that it doesn't keep the new value saved after I refresh the page and keeps reverting to default value product_list which is mentioned in a .tpl file as the following statement: <div id=product_list" class="clearfix">.
Any suggestions on this matter would be greatly appreciated!
If you want data to persist between page refreshes, then you have to store it somewhere persistent.
That could be somewhere that is accessible client side (e.g. in a cookie) and then reapplying the changes based on the data there when the page reloaded.
Alternatively, you could use Ajax to inform the server of the change and have the server store the data somewhere (such as in a database) and generate different based on that data when the page is requested.
(The id, however, is a terrible thing to change dynamically).
Short answer: Not possible (at least, not as in your example).
Javascript is client side and when you refresh the page will be created server side.
If you really want this you should save the value server-side (by using AJAX) and use this value when the page is reloaded. But I really don't think you want to be doing that just to swap id's (which is strange in the first place).
What you are doing happens only on client side. It does not change your .tpl file. In order to get it updated you should send information on server, for instance with ajax, and save new id into database using PHP (as I understand, that is server side language you are using) and populate it in your .tpl file next time page is loaded.
At the same time - not clear why do you need to change an ID of an element that way. Looks like you are doing something wrong.
Client goes to example.com/form.html where a html POST form is displayed
Client fills the form with specific information and submit it to example.com/form.html
When example.com/form.html receives the POST request redirects the Client on example.com/redirected.html
Is possible to retrieve the variables that the client filled and POSTed to example.com/form using javascript ? The javaScript being deployed on example.com/redirected.html only . I presume that can be some "back" controls iframes and ajax involved but I couldn't find a reliable solution yet.
The operation will take place on the same domain so no cross domain issue is involved
Nope, I don't think this is possible.
You have to either
Store the posted value in a cookie
Store the posted value in a session variable on server side
Add the posted value as a GET parameter to the new URL
Use GET to post the original form, and painfully extract the posted value from document.referer on the new page
With HTML5, use localstorage. (The answer describes how to store object in localstorage- you could store an object of your form fields in there).
But you have to store the data on posting with js at example.com/form.html and then can get it on example.com/redirected.html. Without js at form.html, this is not possible with this method.
This works if you plan to use html5 and do not store too much data in it, iirc it has a limit of 5-10mb depending on the browser.
I don't think there is a way to do this by using plain html. With some server-side language (like PHP) it can be done with no problem.
I have been in a similar situation before, and the way I managed to give the data to JS is by including the data in a tag while preparing the output using PHP.
Assuming the redirected to php script receives the POST data from the script it's being redirected in. I would include this in the php code:
<?php
echo '<script type="text/javascript">';
echo 'var postData = '.json_encode($_POST).';';
echo '</script>'
?>
This will have the javascript know what the POST values contained.
To access the values from js:
//assuming you need the value for $_POST['foo']
var foo = postData.foo;
// or if json is encoded as just an associative array
var foo = postData['foo'];
If the POST data is not being passed to the redirected to script (haven't checked if this happens automatically), you could write the $_POST data in a $_SESSION variable, in the first php script:
<?php
$_SESSION['postdata']=$_POST;
?>
and then get it back from SESSION from the redirected to script.
<?php
$postdata = $_SESSION['postdata']; //use this to create the inline script in the html
unset($_SESSION['postdata']; //empty this from the SESSION variables
?>