PHP表单消息

时间:2015-08-26 10:38:47

标签: php html forms email

所以我有一个表格:

<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()" />

发送电子邮件的代码:

<?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;
}
?>

最后:

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

因此,当发送电子邮件时,sent_mail设置为&#39; true&#39;因此,谢谢你的信息应该得到回应。但是现在这不起作用。电子邮件首先发送,但感谢信息不会显示。当按下提交按钮时,我基本上只需要一个感谢信息就可以出现在页面的某个地方。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

mail函数返回一个布尔值(true / false),所以你可以这样做

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!";
}

此外,mail(参数)的结构是地址,主题,消息。这意味着您当前的主题是电子邮件地址,我不确定这是否符合您的意图?

答案 1 :(得分:0)

使用

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

最后:

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