php - How to display message from contact form on the main page

477

I have PHP contact form and I want the Message sent or failed message to be displayed inside the main page.. I have tag after the submit button with id="notification" I want the message from send.php to be displayed inside of it.

My code is:

<form method="post" action="send.php">

        <label>Name</label>
        <input name="name" placeholder="Type Here">

        <label>Email</label>
        <input name="email" type="email" placeholder="Type Here">

        <label>Subject</label>
        <input name="subject"  placeholder="Type Here">

        <label>Message</label>
        <textarea name="message" placeholder="Type Here"></textarea>

        <button id="submit" name="submit" type="submit">Dergo</button>

        <h3 id="notification" align="center" class="page_speed_1032926890"></h3>
    </form>

Send.php code:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $from = 'From: KontaktFormular'; 
    $to = '[email protected]';
    $headers = "From:" . $email . "\r\n";
    $headers .= "Reply-To: ".$email."\r\n";
    $headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; 

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

if(@mail($to,$subject,$body,$headers))
{
  echo "Mesazhi juaj u dërgua me sukses";

}else{
  echo "Gabim! Ju lutemi provoni përsëri.";
}
?>
933

Answer

Solution:

Put your code in a PHP file. Your code:

<?php session_start(); ?>

 <form method="post" action="send.php">

    <label>Name</label>
    <input name="name" placeholder="Type Here">

    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">

    <label>Subject</label>
    <input name="subject"  placeholder="Type Here">

    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>

    <button id="submit" name="submit" type="submit">Dergo</button>

    <h3 id="notification" align="center" class="page_speed_1032926890">
     <?php
           if(!empty($_SESSION["notification"])){
                echo $_SESSION["notification"];
                unset($_SESSION["notification"];
            }
      ?>
    </h3>
</form>

Your send.php file:

<?php

session_start();

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = 'From: KontaktFormular'; 
$to = '[email protected]';
$headers = "From:" . $email . "\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; 

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if(@mail($to,$subject,$body,$headers))
  {
   $_SESSION["notification"] =  "Mesazhi juaj u dërgua me sukses";

  }else{
   $_SESSION["notification"] = "Gabim! Ju lutemi provoni përsëri.";
   }
 header("location: YOUR_PHP_FILE_WITH_THE_FORM ");
 ?>
385

Answer

Solution:

Change this part of your code:

}else{
  echo "Gabim! Ju lutemi provoni përsëri.";
}

to:

}else{
      header('Location: mainpage.php?msg=1');
    }

Then on main page, you can have:

<?php
if(isset($_GET['msg']) && $_GET['msg'] = 1)
    {
?>

<h3 id="notification" align="center" class="page_speed_1032926890">
    Gabim! Ju lutemi provoni përsëri.
</h3>

<?php
    }
?>
70

Answer

Solution:

The message

$notification = "put your message";

The Javascript:

// Appeding notification to Body
$('#notification').html($notification);

Do this where you do your php check.

Put this css on top:

#notification{
    display: none;
}

Setup your div

<div id="notification"> 

People are also looking for solutions to the problem: php - Not able to Connect to Xdebug on AWS EC2 from Netbeans

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.