0

i have a working php contact form so when my message sends i get a thank you message. the issue is that the message shows at the top of the page when i want it to show above or below the submit button. i tried putting the success code there but then it just shows all the time on load. Is there a way to get it to show where i want it to?

<?php
session_start();

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

 
// 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.';
        }
                        
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Drawings </title>
    <link rel="stylesheet" href="style.css">    
    
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    
</head>
<body>
    <section id="section-1" class="pt-page-moveFromBottom">
        <div id="contact-container">
            <!--arrow up-->
            <div class="nav-arrow" style="margin-top:30px;">
                <a href="gallery.html"><i class="fas fa-chevron-up orange"></i><span class="orange">Gallery</span></a>
            </div>
            
            <!--titles-->
            <h1 id="contact-title">paul<span id="contact-title2">warren</span></h1>
            <h2 id="contact-subtitle">drawings</h2>
            
            <!--contact form-->
            <div id="contact-form-container">
            <p class="contact-text">The unframed prints, C print or Giclée,are available on archival quality paper, in a variety of sizes, may be ordered or further information may be obtained by sending me a message.</p>
                <form id="form" method="POST" action="contact.php">
                    <div class="row2">
                        <div class="col2">
                            <input type="text" id="name" name="name" placeholder="Name" required>
                            <input type="text" id="email" name="email" placeholder="Email" required>
                            <input type="text" id="phone" name="phone" placeholder="Phone">
                        </div>
                        <div class="col2">
                            <textarea id="message" name="message" placeholder="Your message" required></textarea>
                        </div>
                    </div>
                    <div>
                                                <!--SUCCESS MESSAGE HERE-->
                    </div>
                    <div class="row2">                      
                        <button type="submit" id="submit">Send</button>
                    </div>
                </form> 
            ```
Zocee
  • 13
  • 5

2 Answers2

1

You should create a variable like $messageAfterMail and echo it where you want like:

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

And in your HTML template, add the code below.

<div>
      <?php echo $messageAfterMail;  ?>
</div>

That should do the job.

SwissLoop
  • 466
  • 2
  • 11
0

Something like this would work:

Essentially, assigining the text to a variable then using that variable later in the html wrapped with the PHP echo shorthand syntax

<?php
session_start();

$emailResponse = '';
if (isset($_POST['email'])) {
    $to = 'zoeharriso@gmail.com';
    $subject = 'New Message Recieved!';
    $from = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $name = $_POST['name'];


// 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)){
        $emailResponse= 'Your message has been sent successfully.';
    } else{
        $emailResponse= 'Unable to send email. Please try again.';
    }
}

?>


<!DOCTYPE html>
<html lang="en">

<!-- page html... -->
<div class="row2">
    <span><?= $emailResponse?></span>
    <button type="submit" id="submit">Send</button>
</div>
</form>
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • i still have the same issue :/ i'm not sure what i'm doing wrong. i copied both answers and still showing both times :( – Zocee Aug 21 '20 at 20:56
  • @Zocee hmm, could you explain what you mean by "still showing both times"? – Wesley Smith Aug 21 '20 at 20:57
  • @Zocee are you saying its showing on page load before submitting the form and you want it not to? – Wesley Smith Aug 21 '20 at 20:58
  • so i tried both answers posted and the 'message sent successfully' is still showing all the time instead of just on submit – Zocee Aug 21 '20 at 20:59
  • so now the message isnt showing at all :( i tried deleting my css to see if anything was stopping it working and its still not showing – Zocee Aug 21 '20 at 21:10
  • @Zocee whoops, typo on my part, Upate the 2 `$response= ......;` to `$emailResponse= ......;` – Wesley Smith Aug 21 '20 at 21:13
  • @Zocee, hmm, can you double check what you have matches the above? With this code, $emailResponse will be an empty string unless `$_POST['email']` is set and `$_POST['email']` will definitely not be set on a normal page load. Perhaps you've missed that if statement? – Wesley Smith Aug 21 '20 at 21:19
  • i copied the whole thing and replaced mine, thank you for helping, i dont understand what im doing wrong! – Zocee Aug 21 '20 at 21:25
  • @Zocee what does the text say specifically when you load the page without submitting the form? – Wesley Smith Aug 21 '20 at 21:26
  • The unframed prints, C print or Giclée,are available on archival quality paper, in a variety of sizes, may be ordered or further information may be obtained by sending me a message. Name Email Phone Your message Your message has been sent successfully. Send thats all the text on the page when i load it – Zocee Aug 21 '20 at 21:26
  • Odd, what do you get if you put `echo $_POST['email']; die();` at the top of the file? – Wesley Smith Aug 21 '20 at 21:29
  • at the top as in before the – Zocee Aug 21 '20 at 21:31
  • just after that – Wesley Smith Aug 21 '20 at 21:31
  • it stays the same – Zocee Aug 21 '20 at 21:34
  • Something is wrong with this test. Are you certain the file you're working on is actually being served? That `die()` should kill everything after it, you should only get the output from `echo $_POST['email'];` and nothing else – Wesley Smith Aug 21 '20 at 21:36
  • i tried it again, the page just loads my email and nothing else but when i go back into the code to remove it its gone, is that normal? – Zocee Aug 21 '20 at 21:38
  • Hmm, Im not sure how `$_POST['email']` could be set to your email if you're not submitting the form. What's the url youre using to load the page? does it have parameters in it? – Wesley Smith Aug 21 '20 at 21:41
  • 1
    its working on the link i just sent you! ok so im guessing everythings working fine now i might just restart everything and clear browsing data! Thank you sooo much youre my hero! – Zocee Aug 21 '20 at 21:45
  • It works as expected for me: [before submission](http://prntscr.com/u3qhlh) [after submission](http://prntscr.com/u3qi46) – Wesley Smith Aug 21 '20 at 21:45
  • 1
    thank you! im not sure why it wasnt working for me when i literally just copy pasted the url! again thank you sooo much! – Zocee Aug 21 '20 at 21:46