Search code examples
phpformsauthentication

How to retain form input through a login process


I have a form which allows users to comment on a page, however they are required to login/register before they can post.

If they make a mistake (e.g. reply is too short) they are told after they have a logged in ('There was an error with you reply'...).

However then the contents of their reply is lost, how can I save this so that it shows up in the form?

The form page is fairly simple:

<?php if (isset($errors['reply_header'])) echo $errors['reply_header']; ?>

<form method="post" action="http://localhost/LOGIN/user/?action=reply">
    <input type="hidden" name="auth_token" value="<?php echo $auth_token; ?>">
    <input type="hidden" name="thread_id" value="<?php echo $id; ?>">
<!--rest of the form goes here, thread_id shows us which thread/page they are replying to-->

This submits to this page:

#   get the register/login controller:
require_once FORUM_ROOT . 'register/index.php';  // if session is not set, then ask for login

if (isset($_GET['action']) )
{
    switch ($_GET['action'])
    {
        case 'new':      # create a new thread...
            require_once USER_ROOT . 'new_thread.php';    
        break;

        case 'reply':
                $_POST['action'] == 'Reply';
                require_once USER_ROOT . 'thread_reply.php';
                die();
        break;

        default:         # show user page...
            require_once USER_ROOT . 'main.html.php';
        break;
    }    
}

I know I can save the contents of the form in a session, but where would I put this?


Solution

  • You doing $_GET but your form method is post

    So instead of $_GET you should use $_POST

    NOTE change your action and make that reply $_GET variable also a

    <input type="hidden" name="action" value="reply" />
    

    Watch if that works