ThorDozer
ThorDozer

Reputation: 104

Refresh page and conserve the post

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

Upvotes: 1

Views: 2072

Answers (3)

Tarik
Tarik

Reputation: 81721

This is how I am doing in my current project:

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

Thanks.

Upvotes: 0

binaryLV
binaryLV

Reputation: 9122

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.

Upvotes: 1

Jeff Lambert
Jeff Lambert

Reputation: 24661

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.

Upvotes: 0

Related Questions