Javascript reload page from server (2021) - javascript

I set a cookie with Javascript. I have a input type checkbox that will switch theme on the website from normal to classic version and so will the cookie name. My backend is PHP. I want to access the newly changed value when clicking the checkbox with PHP, so naturally i want to reload the page so PHP can pick up the new value. Here comes the problem, all the suggestions out there suggest this:
location.reload(true);
But my Visual Studio Code says this is deprecated and indeed, it does not work indeed.
I checked MDN - [https://developer.mozilla.org/en-US/docs/Web/API/Location/reload][1] and it says that i should not pass any params and just use
location.reload();
And yes, using this reloads the page but it seems like it doesn't reload it by requesting the page from the server because my PHP does not pick up the new value. Only when i hit the refresh button manually, it does. And MDN says "The Location.reload() method reloads the current URL, like the Refresh button." which doesn't seem true.
So what can i do here?

You can try reloading page from php using:
header("Location: *your directory*");
If you set the same page you were it will just reload.

Related

Refreshing page via JS snippet

While testing a way in Firefox to reload an HTML page without caching, I've included the following snippet in my code:
<script>
window.setTimeout(function () {
location.reload(false);
}, 5000);
</script>
This reloads the page after 5 seconds, however I get shown the prompt: "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier."
If there a way to do a silent refresh via Javascript, one that doesn't show any prompt? For instance, if I used the refresh Meta tag (HTML), my browser silently refreshes. I want to approximate that same experience, but via JS (and no cache). BTW mine is a Django web app, and I inject the JS code in my Django template.
This is standard behaviour to protect people from submitting form information more than once (eg, prevent double payments in an ecommerce system). Try telling the Javascript to direct to a 'new' page:
Try using this, setting the url to your own;
window.location = "my.url/index.html?nocache=" + (new Date()).getTime();
Answer borrowed from here where there is also an explanation given for why this works -> How can I force window.location to make an HTTP request instead of using the cache?
Have you tried location.reload (true) ? If set to true, it will always reload from server. Set to false it'll look at the cache first.
You are getting this prompt because you ask to reload a POST request. You should always get this prompt when reloading a POST request, as it is not a safe method
However, if you wish, you can explicitely resend a POST request (though you might have difficulties to find back the POST data previously sent). Or explicitely send a GET request to the same URL.

repopulating $_POST variables transparently in PHP

So.
In a LAMP stack (PHP), I've got this situation where I'm showing an intermediate page based on some variable from the first page --more simply, I have one page called, say, ListOfProjects, from which I can select a project to view.
When I go to that project, there are other page-navigation elements (like looking at individual jobs in the project, say) the user can click. Once I click them, and am navigated away from the intermediate page between ListOfProjects and IndividualJob, I have to resubmit the data that got me there.
That's fine, and if I could do it automatically, I would. However, I haven't found a way to force this behavior and eliminate the extra click and the ugly "Confirm Form Resubmission" screen.
Does anyone know a way I could A) silently force form-resubmission when the user hits the back button or B) avoid the situation where there's a form that needs resubmitting?
I've thought about trying to just pass that project ID to the session variable, but it's well within scope to have more than one individual project open in the same browser, which would make that unwieldy.
Thoughts? Suggestions?
Thanks!
Don't use POST.
When you are getting data from the server, use GET and put the data in the query string.
POST is designed for sending data to the server that will make a change (e.g. updating data in the database), it isn't appropriate for just deciding what data to look at.
Some solution is bypass using jQuery to resubmit a form when click on back:
<?php if (isset($_POST['ListOfProject'])): ?>
<form method="POST" id="backToProject"></form>
<script>$("#backToProject").submit()</script>
<?php endif ?>
Another solution is to use header("Location: ...") to force users to redirect a page, BUT you should remove all previous $_POST request using unset($_POST) such as:
unset($_POST);
header("Location: your_uri://your_path");
Try reload a page and reload a page using javascript into <script> tag such as:
if ($_POST['ListOfProject'])
{
echo '<script type="text/javascript">location.reload();</script>';
}
Try to understand GET/POST method: https://en.wikipedia.org/wiki/Post/Redirect/Get
And I don't recommended using link of sites using $_POST method such as say Quentin user.

How can I refresh a webpage without the browser asking if I want to resend again

I got the following code to refresh my webpage. It works great if I don't submit anything with POST but if i do, i get a message from the browser when my webpage refreshes (see image below)
location.reload(true);
I'm not looking for the browser settings tweak. I'm looking for alternative code to refresh without asking.
This is caused due to the page being requested by POST instead of GET. Refreshing will resubmit the POST data. You can force a get using window.location = window.location.href;.
If you want to allow people to reload the page through their browser controls then you will need to implement the PRG pattern which redirects to a GET after a POST.
This will request for a page and not a reload.
window.location = window.location;
GET vs. POST
The solution: don't POST. GET is supposed to be idempotent (up to environmental changes), POST is supposed to be a modification.
After the POST, you should be performing an immediate GET to reload the page with the result of the POST. Then you can refresh at will.
Example:
page_1.html
<form method="POST" action="go.php"> ... </form>
go.php:
<?php
// do stuff...
header('Location: http://www.foo.com/page2.html');
?>
page2.html:
<script type="text/javascript"> location .reload (true); </script>
This is controlled on the browser end and not yours (ie you CAN'T control it). If you want to show the user a page that can be refreshed without the repost option, you'll have to do it via a redirect following the post to a page that you retrieve with a GET not a POST.
Try a javascript redirect. Or alternatively, POST the information using AJAX which will mean refresh reloads the original page, not the results of your ajax post.
you can save your data to window.name and use the window.location = window.location.href trick. and when onload, check if you have data in window.name, get it back and remove it from there. i use the window.name as "session" storage quite often for situations like this.
(I'm spanish speaker, I´ll do my best)
In my case, I have a button to looking for open tickets in a DB, so, i need to press that button every time that I wanna know if there are new ones, I resolved with the next javascript function
<script type="text/javascript" language="javascript">
setTimeout('document.getElementById("btn_busca_ticket").click()', 10000);
</script>
where 10000 is the time between every refresh, (10 seconds)
if you don't have a button, you can create a hidden one with an ID and a submit
(it's not so elegant, but works for me)

jquery ajax tabs question

Is there anyway to stop the tabs from reloading the content on every access? Everytime I click on a tab, the AJAX request is executed again. Also, when I switch my application tabs,the work I'Ve done on my previous app tab is gone since the tab refreshes. Any ideas?
Pretty sure cache: true should work.
Also, see this post which is very similiar to yours.
There's also a cookie parameter you can use to store the selected tab.
See the documentation: http://jqueryui.com/demos/tabs/
And if none of this works for the oddest of reasons, you can always create your own JavaScript cache object such as a simple array and store the tab id and value in there and pull it every time. Further, you could serialize this array to JSON and store it in a cookie that you can check and load on $(document).ready...

Javascript function execution on link click?

I have a link, that when a user clicks on it, it loads a different page as normal but also executes a JS function that autofills a specific text-box on that different page. Is it better practice to use Jquery or Javascript to do this? How can I do this using either one of them?
You can't do this from the source page.
It's a security feature. Imagine if you wrote a JS function that went to an online banking page and auto-filled a bank transfer using the user's current cookie. That's why you can't.
If you control the other page then the sequence you can use is:
Save data to the server;
Go to the new page with a JS redirect;
The new page is loaded from the server;
While loading th epage the data that was saved from the server is retrieved and used to populate the text box.
So it can be done from the server but only if you save it there. The only way of doing that is using Ajax.
An alternative approach is:
Instead of a JS redirect, submit the page back to the server;
The server saves whatever data it needs to;
The server sends back an HTTP redirect to the new page;
The new page uses the saved data to construct the new page with the populated text box.
At the end of the script add return false;. This will make the page run the script without redirecting the page.
Edit: (after saw your edition).
Is it better practice to use Jquery or Javascript to do this? How can I do this using either one of them?
jQuery is a javascript library, this it doesn't matter if you use plain javascript or use jquery as long as you happy with the result.
And about what you say that you successfully manipulated a page fro the redirecter page... I don't see how it possible.

Categories

Resources