同一页上的联系表

时间:2019-07-19 16:02:27

标签: php

我下面有一个联系表,当我提交联系表时,它会在另一页上显示“您的消息已发送!”,但我不想这样做,因此我只需要在联系人内显示一条简单的消息即可。表格上显示“您的邮件已发送”

我在同一页面上找到了许多与提交表单有关的线程,但仍然无法做到这一点,我是PHP和其他后端语言的初学者,因此如果有人能帮助我,将非常感谢。

<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent= " From:  $name, \n Email: $email \n Message: $message";
$recipient = "myemail@gmail.com";
$subject = "New Email";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
echo "Your Message was sent !";
?>
        
             <form method="POST" action="contact-form.php">      

         <input name="name" type="text" class="message" placeholder="Name" />  
 
     <input name="email" type="text" class="message" placeholder="Email"  />

  <textarea name="message" class="message" placeholder="Your Message"></textarea>

             <input type="submit" value="SUBMIT"/>

          </form>

3 个答案:

答案 0 :(得分:0)

提交表单时,已指示其转到“ contact-form.php”。

在contact-form.php中,您具有显示“您的消息已发送!”的代码。响应。

如果您将action属性更改为action =“”,并将代码从contact-form.php移到保存实际联系表单HTML的文件中,然后提供该文件也是.php文件,则应该看到消息显示在页面上,并带有联系表单。

很难不知道其余的设置,但从本质上讲,这就是您要这样做的方式。

答案 1 :(得分:0)

您的代码应为:

<?php 
  if(isset($_POST) && !empty($_POST)){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent= " From:  $name, \n Email: $email \n Message: $message";
    $recipient = "myemail@gmail.com";
    $subject = "New Email";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
    echo "Your Message was sent !";
 }
?>
<form method="POST" action="">
  <input name="name" type="text" class="message" placeholder="Name" />  
  <input name="email" type="text" class="message" placeholder="Email"  />
  <textarea name="message" class="message" placeholder="Your Message"></textarea>
  <input type="submit" value="SUBMIT"/>
</form>

答案 2 :(得分:0)

Your Html Form File and php code file both should be same
also your form action should be call same file

Example :
File Name : contact-form.php

<?php 
if(!empty($_POST['name'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent= " From:  $name, \n Email: $email \n Message: $message";
    $recipient = "myemail@gmail.com";
    $subject = "New Email";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
    echo "Your Message was sent !";
}
?>
<form method="POST" action="contact-form.php">
    <input name="name" type="text" class="message" placeholder="Name" />
    <input name="email" type="text" class="message" placeholder="Email" />
    <textarea name="message" class="message" placeholder="Your Message"></textarea>
    <input type="submit" value="SUBMIT" />
</form>