• Hi,

    I have created a bootstrap form with an input field and a button. I want to display Form values after login on another page. I have added following codes:

    Page 1

    <form method=”POST” action=”test.php”>
    <div class=”form-group”>
    <label for=”reward”>Reward</label>
    <input type=”number” class=”form-control” name=”reward” min=”1″ aria-label=”Amount (to the nearest dollar)” placeholder=”Amount” required>
    </div>
    <button type=”submit” class=”btn btn-success”>Continue</button>
    </form>

    Page 2 (test.php)

    <?php
    if(isset($_POST[‘reward’])) {
    echo $_POST[‘reward’];
    }
    ?>

    With the above codes I am getting the the form values displayed on another page. But I want to add a login process in between.

    Means, If on Page 1, a person entered a “desired amount” in form field and click the button. He should be prompted for login/register. After completing the process of Login/Register he should be redirected to Page 2 and Page two should display the “desired amount”.

    How to do Plz help.

    Thanks

    • This topic was modified 7 years, 8 months ago by Mineshrai.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Managing WP login means using WP resources. You cannot access WP resources if you directly request a PHP page as you do with test.php, which would normally be the place to verify and manage logins. Thus you either need to convert test.php into a custom WP page template (easy) or add in a third page into the process to handle WP stuff. This page needs to be a WP page based on a custom template. There are a couple other possibilities besides a custom page template, but they are more difficult to implement for most people. The alternates are AJAX techniques or passing requests through /wp-admin/admin-post.php.

    With access to WP resources, verify a user’s login status with wp_validate_auth_cookie(). It will return false if not logged in, or the user ID if they are. If not logged in, do a wp_redirect() and exit to the login page. You can include a “redirect_to” URL parameter to wp-login.php to have the user returned to your page after login.

    Thread Starter Mineshrai

    (@mineshrai)

    Thanks @bcworkz

    I have created an endpoint by name “contribute”.

    I have updated the code as below Plz see:

    Page 1

    <form method=”POST” action=”contribute”>
    <div class=”form-group”>
    <label for=”reward”>Reward</label>
    <input type=”number” class=”form-control” name=”reward” min=”1″ aria-label=”Amount (to the nearest dollar)” placeholder=”Amount” required>
    </div>
    <button type=”submit” class=”btn btn-success”>Continue</button>
    </form>

    Page 2 (contribute)

    <?php
    if(isset($_POST[‘reward’])) {
    echo $_POST[‘reward’];
    }
    ?>

    With the above codes I am getting the the form values displayed on another page. But I want to add a login process in between.

    Means, If on Page 1, a person entered a “desired amount” in form field and click the button. He should be prompted for login/register. After completing the process of Login/Register he should be redirected to Page 2 and Page two should display the “desired amount”.

    How to do Plz help.

    Moderator bcworkz

    (@bcworkz)

    Does contribute lead to a WP page being loaded? It’s not absolutely required, but you need some way in a callback to verify a request is the one that requires the user to be logged in. Another issue is when you redirect to the log in page, the POST data would need to be stored in a session variable. Thinking about this more, wouldn’t it be better to verify the user is logged in and do a redirect to log in before the initial form is presented? Then you don’t need to stash the data in a session variable during login.

    The code to check login status and do a redirect if required needs to be in a callback hooked to the ‘wp’ action. When I suggested earlier that this can go on your code page, I was mistaken. I failed to think the process through. The code needs to be in your theme’s functions.php or in a plugin.

    In the callback, first confirm the request is the correct one by any means available. If the contribute endpoint leads to a WP page, you can verify the page ID against the one returned from get_queried_object_id(). Start a PHP session and store the POSTed data in a session variable. Then verify if the user is logged in with is_user_logged_in(). If not, use wp_redirect() to send the user to the log in page. Include a “redirect_to” URL parameter that is equal to the contribute permalink so the user is returned to the contribute page after log in. For example:

    wp_redirect("https://example.com/wp-login.php?redirect_to=https://example.com/contribute/");

    On the contribute page, start a PHP session and get the POST data out of the session variable and proceed as you originally intended.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to display Form values after login on another page’ is closed to new replies.