0

i'm trying to get a message to appear after my form has been submitted, i can get the form to submit and the page to then refresh but unsure how to add the message after the refresh.

i have no knowledge of PHP so sorry is its and obvious answer

thanks

my html & PHP if needed

<form action="action.php" method="POST" target="my_iframe" onsubmit="setTimeout(function(){window.location.reload();},10);">
                    
                        <input type="text" id="name" name="name" placeholder="name" required="required">
    
                        <input type="email" id="email" name="email" placeholder="email" required="required">
                
                        <input type="tel" id="phone" name="phone" placeholder="phone">
                        
                        <div class="form-row" style="display:none;"><input type="hidden" name="url" placeholder="URL"></div>
                
                        <textarea id="message" name="message" placeholder="message" style="height:200px"  required="required"></textarea>
                        
                        <input id="submit" type="submit" value="submit">
                        
                    </form>
                    <iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>



<?php

$to = 'zoeharrisondesign@gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];

//check honeypot
    if( !empty( $honeypot ) ) {
        echo 'There was a problem';
    }
 
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
 
// Compose a simple HTML email message
$message = "$message \r\n |
                From $name \r\n |               
                Tel: $phone";


 
// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your message has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}

?>
Zocee
  • 13
  • 5
  • Check these: https://stackoverflow.com/questions/40968647/how-to-get-the-sucess-message-in-the-same-page-after-submitting-the-contact-form https://stackoverflow.com/questions/5826784/how-do-i-make-a-php-form-that-submits-to-self – Alex Tunbridge Aug 17 '20 at 00:35
  • Does this answer your question? [How to get the sucess message in the same page after submitting the contact form?](https://stackoverflow.com/questions/40968647/how-to-get-the-sucess-message-in-the-same-page-after-submitting-the-contact-form) – FluffyKitten Aug 17 '20 at 01:02

1 Answers1

0

Update your php sending email to this

    if (mail($myEmail, $subject, $message)){
     $success = "Message sent successful";
    }else{
     $success = "Message Sending Failed.";
    }

Then add this php codes in your html codes add this code to display a message. Put it on top of your form html tag.

<?php
 if (isset($success)){ echo "<div>" . $success . "</div>";}
?>

You may even add some styling on the message if you prefer.

Martian4x
  • 47
  • 12