开发者

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 ( $_SERVE开发者_高级运维R['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


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.


This is how I am doing in my current project:

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

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜