2016-04-21 163 views
0

我從來沒有與PHP工作過,並試圖使我的聯繫表單功能與我通過谷歌獲得的一些PHP代碼的幫助。麻煩發送電子郵件通過聯繫表格與PHP

首先我需要將它鏈接到我的html頁面(比如css/javascript),或者如果它共享相同的文件夾,它可以正常工作嗎?

我已經修改了某些部分的名稱以匹配我的html中的名稱,但無法使其正常工作。當我點擊提交按鈕,我得到一個頁面說...

"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Deadline: "; $Body .= $Dealine; $Body .= "\n"; $Body .= "Interested: "; $Body .= $Interested; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print ""; } else{ print ""; } ?> 

php代碼如下。

<?php 

$EmailFrom = "[email protected]"; 
$EmailTo = "[email protected]"; 
$Subject = "contact-form"; 
$Name = Trim(stripslashes($_POST['Name'])); 
$Company = Trim(stripslashes($_POST['Company'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Deadline = Trim(stripslashes($_POST['Dealine'])); 
$Interested = Trim(stripslashes($_POST['Interested'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation 
$validationOK=true; 
if (!$validationOK) { 
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; 
exit; 
} 

// prepare email body text 
$Body = ""; 
$Body .= "Name: "; 
$Body .= $Name; 
$Body .= "\n"; 
$Body .= "Company: "; 
$Body .= $Company; 
$Body .= "\n"; 
$Body .= "Email: "; 
$Body .= $Email; 
$Body .= "\n"; 
$Body .= "Deadline: "; 
$Body .= $Dealine; 
$Body .= "\n"; 
$Body .= "Interested: "; 
$Body .= $Interested; 
$Body .= "\n"; 
$Body .= "Message: "; 
$Body .= $Message; 
$Body .= "\n"; 


// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); 

// redirect to success page 
if ($success){ 
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact-thanks.php\">"; 
} 
else{ 
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; 
} 
?> 

這裏是我的html

<div role="form" class="form-wrapper"> 
<form name="contact-f" action="contact-form.php" method="post"> 
<div class="f-width"> 
<div class="f-col-1 f-group-1"><input type="text" name="Name" class="c-back input-t f-col-1" placeholder="Name" /></div> 
<div class="f-col-1 f-group-2"><input type="email" name="Company" class="c-back input-t f-col-1" placeholder="Company Name" /></div> 
<div class="f-col-1 f-group-1"><input type="text" name="Email" class="c-back input-t f-col-1" placeholder="Email Address" /></div> 
<div class="f-col-1 f-group-2"><select name="Deadline" class="c-back input-t f-col-1"><option class="opt-f">Do you have a Deadline?</option><option value="Not yet" class="opt-f">Not yet</option><option value="Less than 1 Month" class="opt-f">Less than 1 Month</option><option value="2-3 Months" class="opt-f">2-3 Months</option><option value="3-6 Months" class="opt-f">3-6 Months</option><option value="6+ Months" class="opt-f">6+ Months</option></select></div> 
<div class="f-col-2 f-group-3"><select name="Interested" class="c-back input-t f-col-2"><option class="opt-f">What are you interested in?</option><option value="Branding" class="opt-f">Branding</option><option value="Print Design" class="opt-f">Print Design</option><option value="Illustration" class="opt-f">Illustration</option><option value="Website/UI Design" class="opt-f">Website/UI Design</option><option value="Literature" class="opt-f">Literature</option><option value="Video Editing" class="opt-f">Video Editing</option><option value="Other" class="opt-f">Other</option></select></div> 
<div class="f-col-3 f-group-4"><textarea name="Message" class="c-back input-t message-f f-col-3" placeholder="Describe your project..."></textarea></div> 
<div class="f-submit f-group-5"><input type="Submit" name="Submit" value="Send" class="form-btn" /></div> 
</div></form></div> 

(是的,我已經創建了接觸thanks.php文件時,它在最後呼籲)。

+0

這裏的PHP腳本 - 文件名以.php還是.html結尾? – larsAnders

+0

它以.php – ccc

+0

結尾。除了action之外,html中的form標籤還有什麼特性。 – ccc

回答

0

這麼多$身體,怎麼樣:

$Body = " Name : $Name \n Company : $Company \n Deadline : $Deadline \n Interested : $Interested \n Message : $Message"; 

這是你想要的?

+0

只是用較少的單詞就會出現相同的錯誤。感謝您的嘗試。 – ccc

+0

錯誤是什麼意思?告訴我們(我確切地說) – diwang

+0

一旦我點擊提交按鈕,它會指引我到一個空白頁面,其中包含大部分的php文本(原始問題中的第一行代碼)。 – ccc

相關問題