HTML联系表单,PHP代码


HTML contact form, PHP code

我试图使用 PHP 将多个变量从我的 html 表单发送到我的电子邮件。问题是我对PHP一无所知。我找到了一个简单的表格,它只包含 3 个变量、主题、电子邮件和消息。然而,这次我有 7 个。

这是代码。

<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";

$subject = $projecttype;
$to = 'myemail@gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $projectemail'r'n";
$headers .= "Content-type: text/html'r'n";

mail($to, $subject, $body, $headers);

header('Location: thanks.html');
?>

以及相应的 HTML

<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="name">
<label for="email">Email</label>
<input type="text" id="projectemail" name="email">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="phone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="company">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="typeofproject">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="timeline">
<label for="message">Message</label>
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->   

编辑了PHP,省略了正文并将消息放在邮件中,仍然无法正常工作。

<?php
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}";

$subject = $projecttype;
$to = 'albermy145@alberttomasiak.be';

$headers = "From: $projectemail'r'n";
$headers .= "Content-type: text/html'r'n";

mail($to, $subject, $message, $headers);

header('Location: thanks.html');
?>

试试这个,

<?php
$to = 'myemail@gmail.com';
$projectname = htmlentities($_POST['projectname']);
$projectemail = trim(strip_tags($_POST['projectemail']));
$projectphone = htmlentities($_POST['projectphone']);
$projectcompany = htmlentities($_POST['projectcompany']);
$projecttype = trim(strip_tags($_POST['projecttype']));
$projecttimeline = htmlentities($_POST['projecttimeline']);
$aboutproject = htmlentities($_POST['aboutproject']);
$header  = "From: $projectemail 'r'n";
$subject = $projecttype;
$message = "
<html>
<head>
<title></title>
</head>
<body>
<p>Your HTML</p>
Project Name : $projectname
<br/>
Project Email : $projectemail
<br/>
Project Phone : $projectphone
...
...
...
...
</body>
</html>
";
$message .= "";
$header .= "MIME-Version: 1.0'r'n";
$header .= "Content-type: text/html'r'n";
$retval = mail ($to,$subject,$message,$header);
if($retval == true)  
{
header('Location: thanks.html');
exit();
}
?>
}

.HTML

<form id="formproject" action="thank_you_project.php" method="post">
<label for="name">Name</label>
<input type="text" id="projectname" name="projectname">
<label for="email">Email</label>
<input type="text" id="projectemail" name="projectemail">
<label for="phone">Phone</label>
<input type="text" id="projectphone" name="projectphone">
<label for="company">Company</label>
<input type="text" id="projectcompany" name="projectcompany">
<label for="typeofproject">Type of project</label>
<input type="text" id="projecttype" name="projecttype">
<label for="timeline">Timeline</label>
<input type="text" id="projecttimeline" name="projecttimeline">
<label for="message">Message</label>
<textarea name="aboutproject" id="aboutproject" cols="30" rows="10"></textarea>
<input type="submit" id="projectsend" value="Send"></input>
</form>
</div> <!-- end form -->   
将所有

变量添加到$body中(这是mail函数的第三个参数)。

//$body = <<<HTML
//$message <br>
//$projectname <br>
//$projectcompany<br>
//...
//HTML;

或者第二种方法是将这些变量直接添加到第三个邮件参数中。

mail($to, $subject, $message, $headers);
// this code is updated as I wrote in my comment below.
发布

输入值的表单使用"name"属性作为值的标识符,

<form id="formproject" action="thank_you_project.php" method="post">
    <label for="name">Name</label>
    <input type="text" id="projectname" name="name">
    <label for="email">Email</label>
    <input type="text" id="projectemail" name="email">
    <label for="phone">Phone</label>
    <input type="text" id="projectphone" name="phone">
    <label for="company">Company</label>
    <input type="text" id="projectcompany" name="company">
    <label for="typeofproject">Type of project</label>
    <input type="text" id="projecttype" name="typeofproject">
    <label for="timeline">Timeline</label>
    <input type="text" id="projecttimeline" name="timeline">
    <label for="message">Message</label>
    <textarea name="message" id="aboutproject" cols="30" rows="10"></textarea>
    <input type="submit" id="projectsend" value="Send"></input>
</form>
<!-- end form -->   
    <?php
        $projectname = htmlentities($_POST['name']);
        $projectemail = trim(strip_tags($_POST['email']));
        $projectphone = htmlentities($_POST['phone']);
        $projectcompany = htmlentities($_POST['company']);
        $projecttype = trim(strip_tags($_POST['typeofproject']));
        $projecttimeline = htmlentities($_POST['timeline']);
        $aboutproject = htmlentities($_POST['message']);
    ?>