Neil
Neil

Reputation: 342

Keep text in text field after submit

I'm building a form, and I want that all the inserted values will be kept, in case of form submit failure. This is my code:

<?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = $_POST["name"];

   // Verify $_POST['name'] greater than 4 chars
   if ( strlen($name) < 4 ){
        $error= 'Name too short!';
    }
}
?>

<html>
<head>
</head>
<body>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
       <input type="text" placeholder="Name" id="name" name="name" value=""/>
       <input type="submit" value="submit"/>
    </form>
        <?php 
            echo "<h2>Input:</h2>"; 
            echo $name;
            if($error) {
                // No Update AND refresh page with $name in text field
                echo "<br/>" . $error; 
            } else {
                // Update name in DB
            }
       ?>
</body>
</html>

I would like that name field keeps the inserted input text, after submit. I tried to do with php code in input value but doesn't work. Any ideas?

Upvotes: 2

Views: 21457

Answers (5)

TheFatPanda
TheFatPanda

Reputation: 1

The Updated PHP version I think from 8.0 or 8.1 has a shorter version that looks like this:

<?= $_POST['name'] ?? "" ?>

It checks if "$_POST['name']" is True and if it is it returns its value if not returns "".

Upvotes: 0

You can just echo $_POST['name'] in the value attribute of the input. Make sure you check POST values to avoid XSS. I also put up the update DB function, as you don't need to show the form to the user if the name in longer the 4 chars!

 <?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (isset($_POST['name'])){ //change name content only if the post value is set!
       $name = filter_input (INPUT_POST, 'name', FILTER_SANITIZE_STRING); //filter value
   }
   // Verify $_POST['name'] greater than 4 chars
   if ( strlen($name) < 4 ){
        $error= 'Name too short!';
    } else {
        // Update name in DB
        // Redirect
    }
}
?>

<html>
<head>
</head>
<body>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
       <input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
       <input type="submit" value="submit"/>
    </form>
        <?php 
            echo "<h2>Input:</h2>"; 
            echo $name;
            if($error) {
                // No Update AND refresh page with $name in text field
                echo "<br/>" . $error; 
            };
       ?>
</body>
</html>

Upvotes: 1

Neil
Neil

Reputation: 342

Solved. This is the solution that I was looking for. I added in value tag of input the following:

<?php if (isset($_POST['name'])) echo $_POST['name']; ?>

Therefore input field would look like:

<input type="text" placeholder="Name" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>

Thanks for your responses, helped me.

Upvotes: 8

Mars
Mars

Reputation: 3

If you want to keep all values/inputs for further use you could achieve that with php session values.

Besides - you should use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF']

Mars

Upvotes: 0

Shaunak Shukla
Shaunak Shukla

Reputation: 2347

<?php
$error = "";
$name = isset($_POST["name"])?$_POST["name"]:""; //Added condition

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = $_POST["name"];

   // Verify $_POST['name'] greater than 4 chars
   if ( strlen($name) < 4 ){
        $error= 'Name too short!';
    }
}
?>

<html>
<head>
</head>
<body>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
       <input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
       <input type="submit" value="submit"/>
    </form>
        <?php 
            echo "<h2>Input:</h2>"; 
            echo $name;
            if($error) {
                // No Update AND refresh page with $name in text field
                echo "<br/>" . $error; 
            } else {
                // Update name in DB
            }
       ?>
</body>
</html>

Upvotes: 2

Related Questions