本文介绍了提交后在文本字段中保留文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个表单,并且希望所有插入的值都将保留,以防表单提交失败。这是我的代码:
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>
我希望名称字段在提交后保留插入的输入文本。我试图在输入值中使用php代码,但不起作用。任何想法?
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?
推荐答案
解决。这是我正在寻找的解决方案。
我在输入的值标记中添加了以下内容:
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.
这篇关于提交后在文本字段中保留文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!