[英]span inside input field and remove html text when i submit
我是php的新手,我希望将span显示在输入字段中,并且在提交表单后,我讨厌此*必填字段,并且*继续显示。
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?> </span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female" <?php if (isset($gender) && $gender==="female") {echo "checked";}?>>Female
<input type="radio" name="gender" value="male" <?php if (isset($gender) && $gender==="male") {echo "checked";}?>>Male
<input type="radio" name="gender" value="other" <?php if (isset($gender) && $gender==="other") {echo "checked";}?>>Other
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
您不能将其放置在输入中。
您可以在容器内使用绝对定位将其放置在输入的顶部:
.input-container { position: relative; display: inline-block; } .input-container .error { font-size: 14px; color: red; position: absolute; right: 5px; top: 5px; font-style: italic; } label { display: inline-block; margin-right: 15px; } input[type="text"] { width: 300px; height: 40px; padding: 5px 15px; }
<form method="post"> <div class="field-container"> <label for="name">Name *</label> <div class="input-container"> <input id="name" type="text" name="name" value="" /> <span class="error">* This field should not be empty</span> </div> </div> <!-- repeat for other fields --> </form>
如果您只想在出现错误时显示错误:
<?php if (isset($_POST['name']) && strlen($nameErr) > 0): ?>
<span class="error"><?php echo $nameErr; ?></span>
<?php endif; ?>
在这里,我假设变量没有错误时为空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.