2015-06-16 51 views
1

我試圖將我的表單輸入框收集的信息發送到電子郵件。電子郵件正在發送,但電子郵件未顯示輸入框中輸入的信息。簡單的PHP表單到電子郵件 - 不中繼輸入信息

(這是我得到)

名稱:

編號:

地點:

電子郵件:

我新的PHP和任何意見將不勝感激!

PHP

<?php 

$name = $_POST['name']; 
$email = $_POST['email']; 
$phone= $_POST['phone']; 
$location= $_POST['location']; 

$formcontent = " 
Name: $name \n 
Number: $phone \n 
Location: $location \n 
Email: $email"; 

$recipient = "[email protected]"; 
$subject = "Boston Calling Street Team Form"; 
$mailheader = "From: $email \r\n"; 
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); 
echo "Thank you"; 

?> 

表單代碼:錯誤報告

<form method="POST" action="contact.php"> 
    label>Name:</label> 
    <input type="text" class="inputbox" name="name" required> 

    <label>Age:</label> 
    <input class="inputbox" name="age" required> 

    <label>Location (City/State): </label> 
    <input class="inputbox" name="location" required> 

    <label>Phone number: </label> 
    <input class="inputbox" name="phone" required>       

    <label>E-mail address:</label> 
    <input class="inputbox" name="email" required> 

    <label>What is your favorite flavor of icecream</label> 
    <textarea class="inputbox textinputbox" name="icecream" required></textarea> 

    <input class="streetteamsubmit" type="submit" name="submit" value="SUBMIT FORM"> 

</form> 
+1

完成任何基本的調試,比如'var_dump($ _ POST,$ formcontent)'來查看您收到的內容以及您的內容? –

回答

0

一)打開。在代碼頂部添加以下代碼:

ini_set('display_errors',1); 
ini_set('display_startup_errors',1); 
error_reporting(-1); 

b)php代碼和表單是否在同一個文件中?

然後你應該檢查頁面是否由POST方法發送。訪問者第一次到達contactform將始終處於GET方法中。 (他剛剛到達的頁面上,仍然有填補formfields)

if($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    // visitor has sent the form 
} 

c)你永遠不知道$ _ POST元素是否真的存在這樣你應該檢查與isset()函數功能

// initializing with a default value 
$name = ''; 

// overwrite value if $_POST element exists 
if(isset($_POST['name'])) 
{ 
    $name = $_POST['name']; 
} 

d)在這裏你會看到一個非常基本的帶有驗證的工作表單的例子。

<?php 

// Function that validates your formfields and generate errors 
function validate($naam, $email) 
{ 
    $errors = array(); 

    // validation for name 
    if(strlen($naam) < 2) 
     $errors[] = 'U heeft geen naam ingevuld.'; 

    // validation for email 
    if(!strlen($email)) 
     $errors[] = 'U heeft geen email adres ingevuld.'; 
    else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
     $errors[] = 'U heeft een ongeldig email adres ingevuld.'; 

    // return array with errors 
    return $errors; 
} 

// initialization of variables with default values 
$naam = ''; 
$email = ''; 
$errors = array(); 

// if the form has been submited 
if($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    // overwrite the variables with the $_POST elements 
    $naam = $_POST['naam']; 
    $email = $_POST['email']; 

    // validate the variables 
    $errors = validate($naam, $email); 

    // If there aren't any errors redirect to another page 
    if(!count($errors)) 
    { 
     // Here comes the place to send an email!!! 

     header('Location: thankyou.html'); 
     exit; 
    } 
} 
?> 
<!doctype html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>a titel</title> 
    </head> 
    <body> 
     <ul id="errors"> 
     <?php 
      foreach($errors as $error) 
      echo '<li>' . $error . '</li>'; 
     ?> 
     </ul> 
     <form action="" method="post"> 
      <input type="text" name="naam" value="<?php echo $naam; ?>" /> 
      <input type="email" name="email" value="<?php echo $email; ?>" /> 
      <button type="submit">Verzenden</button> 
     </form> 
    </body> 
</html> 
+0

感謝您花時間寫這篇文章。我有提交的電子郵件發送,但我不知道如何在語法中使用$ name和$ email:我正在使用 //用$ _POST元素覆蓋變量 $ name = $ _POST ['名稱']; $ email = $ _POST ['email']; $ recipient ='[email protected]'; $ subject ='Team Form'; $ mailheader ='發件人:$ email \ r \ n'; $ formcontent ='$ name,$ email'; 和 如果(!計數($錯誤))\t \t \t郵件($收件人,$主題,$ formcontent,$ mailheader)或死亡( 「錯誤!」); header('Location:contact.php'); 退出; } – Wing

相關問題