HTML联系表单,PHP代码

时间:2014-11-22 11:21:09

标签: php html forms

我试图通过使用PHP将多个vars从我的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');
?>

3 个答案:

答案 0 :(得分:0)

将所有变量添加到$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.

答案 1 :(得分:0)

试试这个,

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

答案 2 :(得分:0)

发布的表单输入的值是使用'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']);
    ?>