0

I have a contact form below and when I submit the contact form it sends me in another page saying "Your Message was sent!", but I don't want that so I need to just display a simple message inside the contact form saying "Your Message was sent"

And I found many threads here with submit forms on the same page but still wasn't able to do this and I'm very beginner with PHP and other back end languages so if someone would help me this would appreciate a lot.

<?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>
Naveed Ramzan
  • 3,348
  • 3
  • 22
  • 27
adam08
  • 3
  • 5
  • Possible duplicate of [jQuery AJAX submit form](https://stackoverflow.com/questions/1960240/jquery-ajax-submit-form) – Funk Doc Jul 19 '19 at 16:04

3 Answers3

0

When you submit the form you have instructed it to go to "contact-form.php".

In contact-form.php you have the code to display the 'Your message was sent!' response.

If you change the action attribute to action="" and move the code from contact-form.php into the file that holds the actual contact form HTML, then providing that file is also a .php file, you should see the message appear on the page with the contact form.

It's hard without knowing the rest of your setup but essentially, that's how you'd do it.

Mark Overton
  • 1,802
  • 3
  • 15
  • 28
  • So If I leave the action blank then I would not need contact.php file in a new file but just add the PHP code in index.html file where I have contact form elements right? And if I would add the contact form in PHP then do I need to rename index.html to index.php? – adam08 Jul 19 '19 at 16:10
  • If you leave action blank it will simply refresh the page, which is what you want, and if you move the PHP to index.html then you will need to rename it to index.php so that it can run it. Then it should work for you. – Mark Overton Jul 19 '19 at 16:13
  • oh got it thanks and just the last question, I have other files with HTML, like index.html, about.html, etc and when I will change contact.html to contact.php because I have a contact form in contact.html and would it be okay if I would have some other files .html and contact.php? Because it's a simple website that doesn't need PHP expect contact.php form? – adam08 Jul 19 '19 at 17:11
  • I don’t think it really matters that much if it’s .htmk it .php so I always opt for .php, it doesn’t matter that some are different extensions – Mark Overton Jul 20 '19 at 12:04
0

Your code should be:

<?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>
Naveed Ramzan
  • 3,348
  • 3
  • 22
  • 27
  • Thank you it works but is there any way I can customize "Your message was sent" with a different color"? – adam08 Jul 19 '19 at 18:34
  • if you are using bootstrap then you can make it `
    Your message sent
    ` otherwise you can do like `
    Your Message sent
    `
    – Naveed Ramzan Jul 19 '19 at 18:46
  • I see it works now it's styled, but any idea why it shows the message "You Message sent" even I haven't submitted the contact forms, it shows this message when I open the contact page without submitting and I want to show only on submit? – adam08 Jul 19 '19 at 18:57
  • You may update line `if(isset($_POST) && !empty($_POST)){` to `if(isset($_POST['name]) && !empty($_POST['name])){` – Naveed Ramzan Jul 19 '19 at 18:57
  • updated that code with new code u told me to add and now it shows this error? http://prntscr.com/ohfyjb http://prntscr.com/ohfyqu – adam08 Jul 19 '19 at 19:04
  • well, you need to add isset condition too. please see my comments again. – Naveed Ramzan Jul 19 '19 at 19:14
  • I am new to PHP and still can't solve this as it shows the error still? And I tried this but it doesn't work still? if(isset($_POST['name]) && !empty(isset($_POST['name])){ – adam08 Jul 19 '19 at 19:19
  • ok can you email at `ping@naveedramzan.com` and i will update for you. – Naveed Ramzan Jul 19 '19 at 19:24
  • sent you an email – adam08 Jul 19 '19 at 19:29
  • This is my code as you asked on my email because I deleted your conversation on Gmail and I cannot find it https://codepen.io/beh4r/pen/xvbxeP – adam08 Jul 20 '19 at 16:36
  • HI, I checked your code. You need to update from `if(isset($_POST['name]) && !empty(isset($_POST['name])){` to `if(isset($_POST['name]) && !empty($_POST['name])){` as we are checking in first condition that is set and in second is not empty. – Naveed Ramzan Jul 22 '19 at 04:45
  • It doesn't show the error anymore but there's an issue with Email that I don't receive messages on my Inbox anymore and it was working first with your code but don't know what's the issue maybe it's with the SMTP, but thank you for all :) – adam08 Jul 22 '19 at 21:01
  • yup it could be smtp setting issue. You most welcome Kindly upvote my answer – Naveed Ramzan Jul 23 '19 at 03:40
  • yes it's probably some issues with SMTP and upvoted your question but I need to have over 15 reputations to upvote – adam08 Jul 23 '19 at 14:35
  • ah ok. Better luck next time :) – Naveed Ramzan Jul 23 '19 at 14:45
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>
Avinash Saini
  • 1,035
  • 10
  • 10