簡體   English   中英

跨輸入框並在提交時刪除html文本

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM