在同一页面上的Php邮件表单成功消息


Php mail form successfull message on same page

我在我的页面上使用php邮件表单。它工作得很好,但是当发送电子邮件时,有关成功发布的消息将在新的空白页中打开。我想做的是直接在发送按钮下显示消息,并进行一些格式化。我的代码如下所示。

<form action="contact.php" method="POST" id="contactform_main2" style="font-size:14px">
                    <ol>                    
                      <li>
                        <label for="email">Email</label>
                        <input type="email" id="email" name="email" class="text" required />
                      </li>             
                    ....etc
                      <li class="buttons">
                        <input type="submit" name="imageField" value="Odoslať" class="send" style="cursor:pointer" />                     
                      </li>
                    </ol>
                  </form>

这是PHP

<?php
...geting data
if(
  mail("dominik0109@gmail.com", $subject,
  $sprava, "From:" . $email)){
  echo "<strong>Sucessfully sent</strong>";}
  else {
  echo "Error";}
?>

您可以在我的页面上实时查看它,填写任何内容以发布表单。请问如何解决这个问题?啪��

您只需要将表单发送到同一页面并检查是否传递了 POST 变量。

我的页面.php

<form action="#message" method="POST" id="contactform_main2" style="font-size:14px">
                    <ol>                    
                      <li>
                        <label for="email">Email</label>
                        <input type="email" id="email" name="email" class="text" required />
                      </li>             
                    <!-- ....etc -->
                      <li class="buttons">
                        <input type="submit" name="imageField" value="Odoslať" class="send" style="cursor:pointer" />                     
                      </li>
                    </ol>
                  </form>
<?php
if(isset($_POST['email'])) {
// ...geting data
  echo "<a name='message'></a>";
  if(mail("dominik0109@gmail.com", $subject, $sprava, "From:" . $email)){
    echo "<strong>Sucessfully sent</strong>";
  }
  else {
    echo "Error";
  }
}
?>

如果你根本不想刷新页面,你需要javascript和ajax。