Keep text in text field after submit

感情迁移 提交于 2021-02-07 10:25:42

问题


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?


回答1:


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.




回答2:


<?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>



回答3:


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>



回答4:


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



来源:https://stackoverflow.com/questions/25665391/keep-text-in-text-field-after-submit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!