Php 如何清除span标记中的回声

Php 如何清除span标记中的回声,php,forms,reset,Php,Forms,Reset,所以我对用php制作的表单有一个问题。我在span标记中回显错误消息,我想在单击“清除表单”按钮后清除该消息 这是我的php,用于在未填写字段时确定错误消息 <script> function clearForm(){ document.getElementById("firstName").value = ''; document.getElementById("email").value = ''; document.getElementById("las

所以我对用php制作的表单有一个问题。我在span标记中回显错误消息,我想在单击“清除表单”按钮后清除该消息

这是我的php,用于在未填写字段时确定错误消息

<script>
function clearForm(){
    document.getElementById("firstName").value = '';
    document.getElementById("email").value = '';
    document.getElementById("lastName").value = '';
    document.getElementById("subject").value = '';
    document.getElementById("message").value = '';
    document.getElementsByTagName("span").value = "";
}
</script>
      <?php
      $confirm= "";
      $firstName = $lastName = $Email = $Subject = $Message = '';
      if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if (isset($_POST["firstName"]) && $_POST["firstName"]!=null) {$firstName = $_POST['firstName'];} else {
          $confirm ="Please fill it in";
        }
        if (isset($_POST["lastName"]) && $_POST["lastName"]!=null) {$lastName = $_POST['lastName'];} else {
          $confirm ="Please fill it in";
        }
        if (isset($_POST["Email"]) && $_POST["Email"]!=null) {$Email = $_POST['Email'];} else {
          $confirm ="Please fill it in";
        }
        if (isset($_POST["Subject"]) && $_POST["Subject"]!=null) {$Subject = $_POST['Subject'];} else {
          $confirm ="Please fill it in";
        }
        if (isset($_POST["Message"])&& $_POST["Message"]!=null) {$Message = $_POST['Message'];} else {
          $confirm ="Please fill it in";
        }
      }
      if (isset($_POST["Clearform"])) {
        $confirm= "";
      }

      ?>

函数clearForm(){
document.getElementById(“firstName”)。值=“”;
document.getElementById(“电子邮件”)。值=“”;
document.getElementById(“lastName”)。值=“”;
document.getElementById(“主题”)。值=“”;
document.getElementById(“消息”).value='';
document.getElementsByTagName(“span”).value=“”;
}
这就是它在体内的样子

<form name="contact" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="POST">
<p>Your Name: <br>
<input type="text" name="firstName" id="firstName" value="<?php echo $firstName;?>"><span id="confirm"><?php echo $confirm; ?></span></p>
</form>

问题在于您的
clearForm()
不应该使用
.value
相反,您可以使用
innerHTML
innerText
textContent
。看看他们之间的区别

函数clearSpan(){
让mySpan=document.getElementById('errorSpan');
mySpan.innerText='';
}
这是单击按钮时要清除的文本

清除
这应该就可以了。使用JQUERY.hide()隐藏错误。
$(“#btn”)。在(“单击”,函数(){
$(“#errmsg”).fadeOut(“慢”);
});
#errmsg{
颜色:红色;
}

单击以删除错误。

您的错误
您忘记了包含小的
clearForm
函数的代码,因为这似乎是最重要的部分,什么不起作用。您是指onlclick事件的javascript代码吗?如果是,那么我添加了它,但它仍然是java而不是phpI。我不认为它是java,我假设它是Javascript,这真的很重要。你会说“所以我做了一个小函数,它将变量$confirm设置为no content,但它不起作用。”。。。所以我认为这是相关的。因此更改
document.getElementsByTagName(“span”).value=“”
文档.getElementById(“确认”).innerHTML=“”抱歉,如果这不起作用,那么我想我没有遵循您试图实现的目标:(您好,谢谢您的回答。但是您看到我正在使用php创建错误消息,所以我关心的是如何将错误消息回显到span中,然后清除it@AgnieszkaZimolag在clearForm()中,您使用“getElementsByTagName”获取范围它返回一个数组,而不是单个对象,因此您可以通过“getElementsByTagName('span')[0].innerText”访问它,或者使用“getElementById”获取跨度
<input class="button" type="button" name="Clearform" value="Clear Form" onclick="clearForm();"/>