Thomas
Thomas

Reputation: 79

PHP Form Message

So I have a form:

<form method="post" action="contactus.php?message=ok" name="myForm" autocomplete="off">
    <label for="Name">Name:</label>
    <input type="text" name="Name" id="Name" maxlength="60" required/>

    <label for="email">Email:</label>
    <input type="text" name="email" id="email" maxlength="120" required/>

    <label for="message">Message:</label><br />
    <textarea name="message" rows="20" cols="20" id="message" required></textarea>

    <input type="submit" name="submit" value="Submit" class="submit-button" onsubmit="displayMessage()" />

And the code to send the email:

<?php 
if($_POST["submit"]) { 
  // The message
  $message=$_POST["message"];   
  $email=$_POST["email"];
  // In case any of our lines are larger than 70 characters, we should use    wordwrap()
  $message = wordwrap($message, 70, "\r\n");
  // Send
  mail('myemail.co.uk', $email, $message);
  $sent_mail = true;
}
?>

And finally:

<?php
if (isset($sent_mail)) {
  echo 'Thank you. We will be in touch soon.';
}
?>

So when the email is sent, sent_mail is set to 'true' and therefore the thank you message should be echoed. But right now this isn't working. The email sends first but the thank you message doesn't show. I basically just need a thank you message to come up somewhere on the page when the submit button is pressed.

Any ideas?

Upvotes: 2

Views: 83

Answers (4)

CaPs LoCk
CaPs LoCk

Reputation: 150

You are assigning Boolean to $sent_mail and you set it to True.

<?php if($sent_mail){
echo "Email sent successfully";} ?>

Upvotes: 0

Qirel
Qirel

Reputation: 26450

mail function returns a boolean (true/false), so you can do like this

if (mail('myemail.co.uk', $email, $message)) {
    echo "Thank you. We will be in touch soon.";
} else {
    echo "Something went wrong, the email was not sent!";
}

Also, the structure of mail (the parameters) are to-address, subject, message. Which means that your current subject is the email-address, I'm not sure if this is what you intended?

Upvotes: 2

Yatin Khullar
Yatin Khullar

Reputation: 1590

Instead of isset use simply if

Like this

<?php
 if ($sent_mail) {
 echo 'Thank you. We will be in touch soon.';
 }
else
 echo 'Unale to send message';
 ?>

Upvotes: 2

Harshit
Harshit

Reputation: 5157

Use

if(mail('myemail.co.uk', $email, $message))
    $sent_mail = true;
else
    $sent_mail = false;

And finally:

<?php
 if ($sent_mail) {
 echo 'Thank you. We will be in touch soon.';
 }
else
 echo 'Message cannot be send';
 ?>

Upvotes: 0

Related Questions