0

I'm trying to make a simple form that is validated and errors should be shown. Also, the values of the fields should stay.

I'm using simple routing code to determine which page to show. My problem is that the values of the form always reset when I submit it. I googled a bit and found that when the Request changes, the form values get lost.

That's a small example that shows what I want to achieve:

$route = $_SERVER['REQUEST_URI'];

switch ($route) {
    case '/kontakt':
        ?>
        <form method="POST" action="/kontakt">
            <input type="text" required name="test">
            <input type="submit">
        </form><?php
        break;
}

After submitting the entered value should stay in the field.

So how can I keep the Request when routing to the same route but one time with POST and one time with GET without changing the form value to use the _POST array?

Jnt0r
  • 174
  • 3
  • 16
  • Possible duplicate of [Keep values selected after form submission](https://stackoverflow.com/questions/2246227/keep-values-selected-after-form-submission) – Core972 Jan 20 '19 at 12:43
  • Store it in a [session](http://php.net/manual/en/book.session.php) so you can re-use it later maybe? – Jaquarh Jan 20 '19 at 13:27

2 Answers2

1

Lets first grab which request we need to use to get the request arguments.

$request =& $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;

It would probably be a good idea here to check it is set, if it isn't - just leave it blank.

$name = $request['name'] ?? ''; # PHP 7+
$name = isset($request['name']) ? $request['name'] : ''; # PHP 5.6 >

You can then do your routing

# switch: endswitch; for readability
switch(($route = $_SERVER['REQUEST_URI'])):
    case '/kontack': ?>
        <form method="POST" action="/kontakt">
        <input type='text' value='<?= $name; ?>' name='name' />
        ....
        <?php break;
endswitch;

This will then continuously insert the name back in to the value field. However, if you visit a new page and then come back - it will be gone. If you want it to stay at all times, through-out any route, you can use sessions.

session_start();

# We want to use the request name before we use the session in-case the user
# Used a different name to what we previously knew
$name = $request['name'] ?? $_SESSION['name'] ?? ''; # PHP 7
$name = isset($request['name']) ? $request['name'] : isset($_SESSION['name']) ? $_SESSION['name'] : ''; # PHP 5.6 >

# Update what we know
$_SESSION['name'] = $name;

Note: I showed both PHP 5.6> and PHP 7 examples. You only need to use one based on which PHP version you're using.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
0

When you are getting to the route in the first time, then send a HTML-valueAttribute-variable as null. When you go back to the route after posting send the post value to the HTML-valueAttribute-variable:

When you reach the route the first time:

<?php
    //Value that is sent to the view/page when accessing route without having posted a value
    $testValue=null
?>
    <form method="POST" action="/kontakt">
        <input type="text" required name="test"
            <?php
                if($testValue != null)
                {
                    echo "value='".$testValue."'";
                }
            ?>
        >
        <input type="submit">
    </form>

When you use the route after have posted:

<?php
    //Value that was posted is sent to view/page
    $testValue=$POST['test']
?>
    <form method="POST" action="/kontakt">
        <input type="text" required name="test"
            <?php
                if($testValue != null)
                {
                    echo "value='".$testValue."'";
                }
            ?>
        >
        <input type="submit">
    </form>
Brainmaniac
  • 2,203
  • 4
  • 29
  • 53