通过php文件操作index.HTML中的HTML元素

通过php文件操作index.HTML中的HTML元素,php,email,dom,contact-form,Php,Email,Dom,Contact Form,我制作了一个简单的联系表单,并试图找出如何更改页面上的html元素以创建电子邮件发送确认。到目前为止,如果发送了电子邮件,它只需重新加载页面。如果名称部分为空或输入的电子邮件无效,我还希望错误消息作为html元素显示在输入标签旁边 这是我目前拥有的代码: HTML: <div id="contact-content" class="format-div"> <form id="contact-form" action="/contact-form.php" method

我制作了一个简单的联系表单,并试图找出如何更改页面上的html元素以创建电子邮件发送确认。到目前为止,如果发送了电子邮件,它只需重新加载页面。如果名称部分为空或输入的电子邮件无效,我还希望错误消息作为html元素显示在输入标签旁边

这是我目前拥有的代码:

HTML:

<div id="contact-content" class="format-div">
    <form id="contact-form" action="/contact-form.php" method="post">
        <label for="contact-name">Name</label>
        <input type="text" id="contact-name" name="nameIn" placeholder="Enter 
       your name!">
        <label for="email-address">E-mail</label>
        <input type="text" id="email-address" name="emailIn" placeholder="Enter your e-mail address!">
        <label for="contact-message">Message</label>
        <textarea id="contact-message" name="messageIn" placeholder="All this space to fill with words..."
                  style="height:200px"></textarea>
        <button type="submit" name="submit-mail"><i class="far fa-paper-plane"></i></button>
    </form>
</div>

在HTML标记中,您可以放置以下内容:
因此,如果出现错误,它将显示在HTML标记中

HTML页面的某个地方,基本上位于顶部。如果设置了GET变量,您可能应该捕获该变量。也可以类似地显示错误

if(isset($_GET['mailsent'])){echo "Email Sent";} else if(isset($error)){echo $error;}
另外,如果index.html不是包含表单代码的页面,那么它将无法访问错误变量so

更改
标题(“位置:index.html?mailssent”)
标题(“位置:index.html?error=“.$error.”&mailsent=“”)

因为html页面需要解析php,所以理想情况下它应该是一个php页面,即index.php

header("Location: index.php?error=".$error."&mailsent=");

当然,将index.html页面重命名为index.php。如果您不想这样做,那么就采用JavaScript方法。

我认为您的代码中有一个java脚本部分没有显示出来
header("Location: index.php?error=".$error."&mailsent=");