简体   繁体   English

HTML联系表格,PHP代码

[英]HTML contact form, PHP code

I was trying to send multiple vars from my html form to my email by using PHP. 我试图使用PHP将多个var从我的html表单发送到我的电子邮件。 The problem is that I do not know anything about PHP. 问题是我对PHP一无所知。 I found a simple form which worked for me, but it contained only 3 vars, subject, email and message. 我找到了一个对我有用的简单表格,但其中仅包含3个变量,主题,电子邮件和消息。 This time however I have 7. 但是这次我有7。

Here's the code. 这是代码。

<?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');
?>

And the corresponding 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 -->   

Edited the PHP, left out the body and put message in mail instead, still not working. 编辑了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');
?>

Add all variables into $body (which is the third param of mail function). 将所有变量添加到$body (这是mail函数的第三个参数)。

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

Or the second way is to add these vars into the third mail param directly. 或第二种方法是将这些变量直接添加到第三个邮件参数中。

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

Try this, 尝试这个,

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

the form posted the value of input is using 'name' attribute as identifier of value, 张贴输入值的表单使用“名称”属性作为值的标识符,

<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']);
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM