2011-06-06 10 views
0

我有一個包含2個文本框和1個提交按鈕的文檔。當我按下提交按鈕時,我想發送一封電子郵件,並且它應該是文本框的值。我的PHP知識有點塵土飛揚。我不想用emailproces打擾用戶,所以他不能看到這個,只能重定向。PHP - 通過郵件發送兩個字段

<html> 
<body> 

<div class="section_form" id="usernameSection"> 
<label for="username">Login:</label> 
<input size="20" type="text" name="username" id="username" /> 
</div> 

<div class="section_form" id="emailSection"> 
<label for="email">Email:</label> 
<input size="20" type="text" id="email" name="email" maxlength="20"/> 
</div> 

<div id="submit_button"> 
<button type="submit" value="Submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button> 
</div> 

</body> 
</html> 

非常感謝!

回答

0

將此代碼用於您的任務。

<?php 
if(isset($_POST['submit'])){ 
    extract($_POST); 
    $message = "username : $username ; email : $email"; 
    $to = "[email protected]"; 
    $subject = "Email Subject"; 
    mail($to, $subject, $message); 
    } 
    ?> 

<html> 
<body> 
<form method="POST" name="form1"> 
<div class="section_form" id="usernameSection"> 
<label for="username">Login:</label> 
<input size="20" type="text" name="username" id="username" /> 
</div> 

<div class="section_form" id="emailSection"> 
<label for="email">Email:</label> 
<input size="20" type="text" id="email" name="email" maxlength="20"/> 
</div> 

<div id="submit_button"> 
<button type="submit" value="Submit" name="submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button> 
</div> 
</form> 
</body> 
</html> 
2

使用PHP郵件功能 - 列出here

這應該能夠提供幫助。

1

基本上你要查找的內容的核心是

<?php 
// Check that all fields are present, construct the message body, etc 
mail($to, $subject, $body); 
header("Location: wherever.php"); 
exit(); 
?> 

參見PHP文檔中的mail function。 (另外,謝謝你亞歷克斯爲exit()提醒。)

+0

發送'位置'標題後不要忘記'退出'。 – alex 2011-06-06 12:02:55

+0

好點 - 編輯來反映。 – Shadikka 2011-06-06 12:03:52

+0

位置重定向我的權利?你將如何綁定用戶名和電子郵件到$ message? – CustomX 2011-06-06 12:09:57