PHP Form Message

2020-04-16 05:09发布

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?

4条回答
Fickle 薄情
2楼-- · 2020-04-16 05:17

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';
 ?>
查看更多
对你真心纯属浪费
3楼-- · 2020-04-16 05:28

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?

查看更多
一夜七次
4楼-- · 2020-04-16 05:41

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';
 ?>
查看更多
倾城 Initia
5楼-- · 2020-04-16 05:44

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

<?php if($sent_mail){
echo "Email sent successfully";} ?>
查看更多
登录 后发表回答