1

i want to refresh my webpage automatically and conserv my post variable value.

My only problem is the conservation of my post variable.

I thought about a session but i don't know how to do it.

heres my code..

session_start();
    if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
        $_SESSION['editor'] = $_POST["editor"];

and i refresh by setting my url like this in js -> document.location.href= document.location.href;

thanks

ThorDozer
  • 104
  • 2
  • 12
  • when i refresh my page, my query (my post putted in my session variable) becomes null – ThorDozer Jun 06 '11 at 14:20
  • See my answer. I am preserving the post values as in my example... – Tarik Jun 06 '11 at 14:33
  • @Braveyard, the problem is that `$_POST` gets filled with data from the only on `POST` requests. He is doing a `GET` request by updating `document.location.href`. To keep all values, they have to be either POST'ed (i.e., form must be submitted, but ... what if there are multiple forms? and what if user does not plan to submit the form?) or stored by javascript (must be done in browser, not in server). – binaryLV Jun 06 '11 at 14:40

3 Answers3

1

You refresh your page from client-side (browser) by using javascript. It does not perform POST request, and it in no way sends form values to the server.

If you really want to refresh the page like you do, you can store values as cookies, read about working with document.cookie in javascript.

I would also advise you to consider using AJAX. It's rare case when page really needs to be reloaded. Usually it is enough with "reloading" just a small part of the page, and that can easily be done with AJAX.

binaryLV
  • 9,002
  • 2
  • 40
  • 42
0

You can try something similar to this (at the top):

session_start()
foreach($_POST as $k => $v)
    $_SESSION['post_'.$k] = $v

Then the POST variables will be available in the SESSION array. At the bottom, I would do this:

foreach($_SESSION as $k => $v)
    if(strpos($k, 'post_') !== false)
        unset($_SESSION[$k]);

This way, if the user navigates away from the page the session will be clear of the post.

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
0

This is how I am doing in my current project:

<input type="text" name="username" value="<?php echo isset($_POST["username"]) ? $_POST["username"] : '' ?>" /> 

Thanks.

Tarik
  • 79,711
  • 83
  • 236
  • 349