drkbpwk609294 2016-10-19 18:34
浏览 18

如何在联系表单提交中显示成功/失败消息

I'm trying to figure out a simple way to to show that a message was either sent or not in a contact form. At the moment on success or failure it is redirecting to a designated success or failure page. All I'm looking to do is instead of redirect, maybe show a div, or modal or popup but I'm not sure how to do that in PHP.

Form:

<div class="col-lg-7 col-sm-7 contactboxform">
    <form action="contact_me.php" method="POST" id="contactform">
      <input type="text" name="name" placeholder="Name..." class="input-text">
      <input type="text" name="phone" placeholder="Phone Number..." class="input-text">
      <input type="email" name="email" placeholder="E-mail Address..." class="input-text">
      <input type="checkbox" name="interested">
      <textarea name="message" placeholder="What would you like to say?" class="input-text text-area" rows="7"></textarea>
      <button type="submit" id="subbtn" class="btn btn-default btn-lg">Submit</button>
    </form>
    </div>

PHP:

// send email
if (!empty($name) && !empty($email) && !empty($phone) && !empty($message)) {
    $success = mail($emailTo, $subject, $body, "From: <$emailFrom>");
}
if ($success){
    header('Location: success.html');
}
else {
    header('Location: failed.html');
}
//redirect to success page

So I'm just not sure how to properly say in PHP that on success, show specific div / popup.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • duanliang9288 2016-10-20 02:12
    关注

    You can try creating a div and then inserting into it a response from a AJAX request.

    For example, add to your HTML code:

    <div id="form_status"></div>
    

    When the submit button is clicked, you perform the AJAX request:

    <button type="submit" id="subbtn" class="btn btn-default btn-lg" onClick="validateForm();">Submit</button>
    

    And the validateForm function can be:

    function validateForm()
    {
        var status = $('div#form_status');
        var form = $('form#contactform');
        $.ajax({
            url: 'your-php-file.php?'+form.serialize()
        }).done(function(data){
            status.html(data);
        });
    }
    

    On your-php-file.php, you can access all the data from the form with $_GET['input_name'], with input_name being the name of the input on the form.

    With this, anything that you print on the your-php-file.php will be printed into the div#form_status, without the need to refresh the page.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等