2014-11-22 63 views
0

我想通過使用PHP將多個變量從我的html表單發送到我的電子郵件。問題是我對PHP沒有任何瞭解。我找到了一個簡單的表單,但它只包含3個變量,主題,電子郵件和消息。但是這一次我有7.HTML聯繫表格,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 = '[email protected]'; 

$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 = '[email protected]'; 


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


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


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

你能顯示你的html嗎? – peppeocchi 2014-11-22 11:24:46

回答

0

嘗試此,

<?php 
$to = '[email protected]'; 
$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 --> 
+0

當我使用此代碼時,我發送到一個空白頁面,並收到一封電子郵件,說明由於沒有收件人而發送失敗。 :/ – 2014-11-22 11:55:57

+0

對不起,需要添加* $到='[email protected]'; * – ARJUN 2014-11-22 11:57:44

+0

我試過你的方法,但是現在當我收到郵件時,我只能得到字符串(比如Project Name:),沒有主題也沒有收件人:/我不知道有什麼問題我看着PHP和HTML,希望能找到某種錯誤,但我找不到任何東西 – 2014-11-22 12:38:17

0

添加所有變量成$body(這是mail函數的第三個PARAM)。

//$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. 
+0

因此,如果我將所有變量放入消息中,然後僅在消息體中發佈消息是不夠的。 – 2014-11-22 11:29:27

+0

你把它們放到'$ body'(我的第一個代碼段),或者直接到'mail'函數(第二個片段)。兩者都可以工作(我只使用兩個變量來向你展示如何,你也需要添加其他變量)。 – panther 2014-11-22 11:31:38

+0

謝謝你,我會定義嘗試它。 – 2014-11-22 11:33:28

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