douju9272 2018-09-04 00:46
浏览 50

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

I made a simple contact form and I am trying to figure out how to change the html elements on the page to create a confirmation for when the e-mail is sent. As of now, it just reloads the page if an e-mail is sent. I would also like error messages to appear beside the input label as an html element if the name section is blank or e-mail entered is not valid.

This is the code that I have currently:

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>

PHP:

if (isset($_POST['submit-mail'])) {

    $name = $_POST['nameIn'];
    $address = $_POST['emailIn'];
    $message = $_POST['messageIn'];
    $error = "";

    if($name == '') $error .= 'Please fill in your name';

    if (!filter_var($address, FILTER_VALIDATE_EMAIL)) $error .= 'The e-mail is invalid';

    $mailTo = "myemail@mysite.come";
    $subject = "FROM WEBSITE: ".$name;
    $header = "Mail from: ".$name.", ".$address."

";

    mail($mailTo, $subject, $message, $header);
    header("Location: index.html?mailsent");
}

What I am going for with error messages

  • 写回答

2条回答 默认 最新

  • duancan9815 2018-09-04 01:25
    关注

    Inside a HTML tag you can put this: <php if (isset($_POST['submit-mail'])) { echo $error; } ?> so if theres an error it will show inside the HTML tag

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c