0

我正在尝试设置一个简单的联系表格。一切的样式都正确,但是当我点击提交时它不会带我去任何地方,只是尝试打开contact.php。我认为实际发送消息的代码中缺少一些东西。我敢肯定,我错过了一些相当简单的事情,但这有点超出我的想象。任何帮助表示赞赏。

<form action="mail.php" method="POST">
    <p>Name</p> <input type="text" name="name">
    <p>Company</p> <input type="text" name="company">
    <p>Email</p> <input type="text" name="email">
    <p>Phone</p> <input type="text" name="phone">
    <p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
    <input type="submit" value="Submit">
    </form>

<?php

$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";

$to ='______@gmail.com';
$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email 
// display message "We've received your information"

if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>

编辑:在添加 mail.php 的完整 url 后,我终于能够收到一封电子邮件......但是除了消息之外,没有任何信息被包括在内。发件人被列为 Apache...我如何确保在表单中输入的信息将包含在电子邮件中?感谢迄今为止的所有帮助。

4

1 回答 1

0

由于您在同一页面中指定代码,因此您可以action在表单中省略。此外,在脚本运行后设置运行 PHP 的条件。

<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";

$to ='______@gmail.com';
$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email 
// display message "We've received your information"

if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
} }
?>
<form action = "<?= $_SERVER["PHP_SELF"]; ?>"method="POST">
    <p>Name</p> <input type="text" name="name">
    <p>Company</p> <input type="text" name="company">
    <p>Email</p> <input type="text" name="email">
    <p>Phone</p> <input type="text" name="phone">
    <p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
    <input type="submit" value="Submit" name="submit">
    </form>

希望能帮助到你!

编辑:我不确定当前文件名是什么,但为了保持动态,我提到$_SERVER["PHP_SELF"]了不推荐的用法。不过,试试吧!:)

于 2013-02-21T05:24:59.417 回答