Session save delayed in php - php

I have the following problem: I have two pages. In the first page is a button. When the button is clicked, an ajax request is made to a page that saves data to the session. Then, it redirects me to the second page in which I display this saved data. The problem is that when I go to the second page the data is not displayed, but when I refresh the page the data is displayed. How can I redirect to the second page with the session data stored?
Example:
Before Refresh:
After:
Does anyone have any ideas?

You should wait for the response of the AJAX request before you forward to the next page.
Preferable use the callback method to redirect to the next page.
If you post some code may be it will be easier to point out.

Try this, hope it'll work
if you are using jQuery then fine, use e.preventDefault(); and redirect your next page after getting the ajax response.

Related

Search in a modal, update DIV on original page

It's another late night and another seemingly simple issue that's causing a headache!
So, here's the situation. I have a simple HTML form that's in a Bootstrap modal. When this form is submitted, there's an AJAX POST to a receiving page, SESSIONS are set and the request is then forwarded to a simple DB query. This all works.
What I want to do is show the sessions on the original page without a page refresh.
I thought this would be easy so I tried using this on the original page;
$('#filteroptions').on('hidden.bs.modal', function () {
$("#breadcrumbs").load('includes/files/private/breadcrumb.php');
});
breadcrumb.php holds the output format and the file is populated immediately after the POST from the modal (called filteroptions)
I also tried to attach it to the POST success with a simple success process to load the file but each time, the breadcrumb.php fils to be loaded.
Curiously, if I ctrl+F5 the page after the first POST, there is no value shown BUT if I search again the DIV is updated each time I search after that.
Why would the request not fire the first time that the search is performed? Why do I need to refresh the page for everything to start working?
There is no caching to it's not a case of a dependantr file being cached after the refresh.
Thanks
The solution was to populate the div with nothing and then update it.
Previously, the div was only being drawn when it was populated thanks to the code in the breadcrumb file looking for a specific POST or SESSION variable.
It now allows for a blank value.

GET rid of a page from browser history

I have a page for example, say first.html. it has a form with action set to, say xyz.php.
This php file ,after processing , sends a header () with location . Now browser fetches the page.
Now when I click back button ,first.html shows.
Is there a way to get rid of that first.html page and show other page from history?
When there's a redirect if you want to replace the page that triggered the redirect you can use window.location.replace(newUrl).
Here's the documentation for replace

How to resubmit PHP form with javascript

I am wondering if it is possible to to resubmit a form with a button click that calls up a javascript command.
So this is basically what I'm trying to do -
page 1: form; action = page2.php
page 2: generate a randomized list according to parameters set by page 1
I would like to place a button on page 2 so that on click, it would be as if the user has hit F5, and a new list would be generated with the same parameters.
I found a lot of help on Google with people trying NOT to get this to happen, but I'm not sure how to actually get it to happen.....
Thank you!
You could use location.reload(true); to refresh the page.
<button onclick="location.reload(true);">refresh</button>
you only need to use. location.reload() instead of location.reload(true). since this will not disable caching for the reload but using true as the parameter will.
here is the reference. How to refresh a page in jquery?
EDIT: why not just use hidden input field and preserve the values and fetch it anytime you want to?

how to post data from one php to another without reloading it?

I have a html page with two iframes in them .there is a button on first iframe which should reload second iframe with some data which is sent on the click of the button ..but the first iframe containing the button should not reload.how can i make it possible ?
<?php
// code to send data without reloading page but instead reloading another iframe with sent data?>
In the first iframe, use javascript to submit the form by some sort of ajax method:
<form [...] onsubmit="sendAjax(); return false;">
The return false part prevents the form from submitting (as long as you don't have some sort of errors in your sendAjax() function.
Since it is going to take a bit of time for the server to process the request that should show up when iframe 2 refreshes, you could possibly use evilone's suggestion and use sessions to see if some sort of session variable has been set. Then in frame 2, make the javascript wait say 500 ms before refreshing and then refresh the page. You could loop this refreshing process to make sure that the session variable has been set and that the correct output will be sent.

Prevent script from re-executing action on page refresh

I am building my own php cart for the past week and I got stuck in some issues.
I managed to add new items in the cart, and the URL looks like below.
http://blah-blah.com/order/index.php?action=add&id=84
The question is simple: How can I prevent from adding the same item again in the cart
if someone refreshes the page? Cause now every time someone refreshes the page, it changes the quantity to +1 for the specific item..
Also, after moving to the checkout page, if they press the back button in the browser, AGAIN the quantity will change to +1;
Any recommendations?
Yea. Have them set, rather than increment the quantity.
Also, you'd usually use POST (not GET) requests for this sort of action. Browsers know this and cunningly ask the user whether they want to re-submit POST data.
The cleanest approach for your user may be to do the whole thing with AJAX. If they go "back", they'll just go back to the last page they visited without trouble. This would be equivalent to how comments on Stack Overflow are submitted: you cannot go "back" to your submission step and end up writing duplicate comments.
I would suggest following steps to avoid multiple submission and back button issues:
As suggested above you should use POST method to submit your form instead of GET.
Prevent multiple form submission from the same session as described here: http://phpsense.com/php/prevent-duplicate-form-submission.html
After you're done with processing the POST request you should redirect to a confirmation URI OR better to the same URI as follows:
// redirect after processing the POST request
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
As said by Tomalak it's better to use POST datas for thoose kind of action.
An other point you can use in conjunction with POST datas is to respond to the user with a redirect header then even if he try to refresh or go back the data won't be processed again.
basic example:
<?php
if( !empty($_POST) ){
// do stuff with POST datas then
header("location: mypage.php\n");
exit;
}
You can use
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
To redirect your user to the previous page? Pressing refresh or the back/forward buttons will not load the add page again.
I know this question is closed, But i have an alternative that I thought up for this exact problem.
The idea is to create a hash of $_POST data before and after. For this example i used md5, but you can use whatever algorithm you want:
$postHash = md5(serialize($_POST).date("dMY",time()));
if(!isset($_SESSION['post'])) $_SESSION['post']="Not yet set.";
Then all your processing code that would normally go at the top of the page you simply put into the following IF:
if($_SESSION['post']!=$postHash) {
// Perform actions
} else {
echo "Cannot post duplicate data";
}
Then, at the bottom of the page (or after processing has complete, and any database calls have been actioned):
$_SESSION['post'] = md5(serialize($_POST).date("dMY",time()));
There are still a few problems but it seems to work okay. Hope this helps.
Hi I don't have a coding solution for you but rather a logical solution to this problem give it a try it might help you, it helped me
I faced the same problem where I used the "ShoppingCart?AddToCart=1&....." in my URL(GET Variable) as a way of knowing that the user wants to add products to the cart and every time I refreshed the page the same product got added again and again
My way of working when I received the AddToCart Request was to first execute a method that will add the product to cart and then execute another method that will display the contents of the cart (THIS WAS THE PROBLEM RIGHT HERE)
I changed the logic a bit
Now whenever I receive the same AddToCart Request I execute the same first method to add the contents to the cart
BUT
Now rather than executing the second method to display the content of the cart I use the header("Location: ShoppingCart?ViewCart=1")
This means that after executing the first method which add the content to the cart I make a ViewCart Request to my Shopping Cart page, now every time I make a AddToCart Request it properly adds the contents to the cart and after that whenever I try to refresh the page to try to replicate the AddToCart Request I always end up with ViewCart Request and the Page simply shows the Cart contents and not add any futher contents to the cart
The trick over here is that every time I receive the "ShoppingCart?AddToCart=1&....." I execute the Add function and the header function in such a way that I end up with displaying "ShoppingCart?ViewCart=1" in the URL rather than the original "ShoppingCart?AddToCart=1&....."
Now even if some tries to refresh the page or comes back to the page using Back Button in the browser they always end making a "ShoppingCart?ViewCart=1" Request, they never see the "ShoppingCart?AddToCart=1&....." in the URL
like I said I don't have a Coding Solution but rather a Logical Solution
Hope this helps

Categories