1

Is there any way to store the value in session on page refresh?

For e.g. If I write the "abc" in textbox, and when I refresh the page, it will store in session.

Updated: Actually, If I write the "abc" in textbox then I will go on 2nd page of pagination without ajax (that is on-loading). When I come back to 1st page, the value of textbox remain.

This is what I want to say.

Viral Bhoot
  • 357
  • 3
  • 17
  • You would need to post the data to the server to add it into session. What exactly do you mean by refresh the page? By submitting a form, or just hitting f5? – Steve Nov 24 '15 at 13:04
  • Actually, If I write the "abc" in textbox then I will go on 2nd page of pagination without ajax (that is on-loading). When I come back to 1st page, the value of textbox remain. This is what I want to say. – Viral Bhoot Nov 24 '15 at 13:08

3 Answers3

3

What have you tried so far? It's very basic question

just session_start(); and $_SESSION['abc'] = $_POST['abc']; if you send it via post and

<input `name="abc" value="<?= isset($_SESSION['abc']) ? $_SESSION['abc'] : 'default value' ?>" >`;

Edit:

If you hit f5 it won't be send as POST, you could check if f5 is hit and send it VIA ajax or you could send input after blur event when input is not empty, there are many possiblities. Check capturing f5 keypress event in javascript using window.event.keyCode in window.onbeforeunload event is always 0 and not 116

Also the other option is to save value in JavaScript in cookie after value is entered or input is blured and then when you are back on the site check if this value exists and show it.

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
  • if I hit F5, how will that be send as post? – davejal Nov 24 '15 at 13:06
  • 1
    If you hit f5 it won't be send as POST, you could check if f5 is hit and send it VIA ajax or you could send input after blur event when input is not empty, there are many possiblities. Check http://stackoverflow.com/questions/14707602/capturing-f5-keypress-event-in-javascript-using-window-event-keycode-in-window-o – Robert Nov 24 '15 at 13:07
  • then I'm missing you ajax part in your answer (as OP also only has the php tag) – davejal Nov 24 '15 at 13:09
  • This appears to be what the OP is looking for - saving the previous forms values when proceeding to the next in a multi part form scenario. No ajax required – Steve Nov 24 '15 at 13:13
  • Though pressing back is not the same as a refresh - usually the browser will cache the submitted values its self: http://stackoverflow.com/questions/1036782/html-form-values-and-back-button – Steve Nov 24 '15 at 13:18
1

I don't think this is possible. A refresh happens on the client side. Without any requests send to the server to be able to process.

To get a request to your server you either need a post or a get and refresh is neither of them.

AS @Robert already suggested in his comment, you will need more then just php to solve your problem (i.e. ajax)

davejal
  • 6,009
  • 10
  • 39
  • 82
1

im not sure i just found this maybe it can help you. You can use jquery and ajax. Using jquery you can detect if page is refreshed

$('body').bind('beforeunload',function(){
   //get your textbox value and send it to php file and you can start your session there.
});

Link

Community
  • 1
  • 1
Tariq Husain
  • 559
  • 5
  • 23