2016-04-30 25 views
0

我有一個php文件,下面的代碼。當我點擊提交時,我似乎遇到錯誤。如何將此表單數據發送到我的電子郵件?

錯誤只是一個重定向到錯誤頁面在PHP代碼中具體引用「Your Name」導致錯誤 但我不能解決如何解決它。

PHP代碼是在這裏|

<?php 
/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* Check all form inputs using check_input function */ 
$name = check_input($_POST['inputName'], "Your Name"); 
$email = check_input($_POST['inputEmail'], "Your E-mail Address"); 
$subject = check_input($_POST['inputSubject'], "Message Subject"); 
$message = check_input($_POST['inputMessage'], "Your Message"); 

/* If e-mail is not valid show error message */ 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
show_error("Invalid e-mail address"); 
} 
/* Let's prepare the message for the e-mail, but might not work for drop down subject...just try */ 

$subject = "Someone has sent you a message"; 

$message = " 

Someone has sent you a message using your contact form: 

Name: $name 
Email: $email 
Subject: $subject 

Message: 
$message 

"; 

/* Send the message using mail() function */ 
mail($myemail, $subject, $message); 

/* Redirect visitor to the thank you page */ 
header('Location: http://www.address.com/sucesssubmit.html'); 
exit(); 

/* Functions we used */ 
function check_input($data, $problem='') 
{ 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) 
{ 
show_error($problem); 
} 
return $data; 
} 

function show_error($myError) 
{ 
?> 
<html> 
<body> 

<p>Please correct the following error:</p> 
<strong><?php echo $myError; ?></strong> 
<p>Hit the back button and try again</p> 

</body> 
</html> 
<?php 
exit(); 
} 
?> 

形式是這裏

<form role="form" id="contact-form" method="post" action="send_form_email.php"> 
         <div class="form-group"> 
          <label for="name">Your Name</label> 
          <input type="text" name="name" class="form-control" id="name" /> 
         </div> 
         <div class="form-group"> 
          <label for="email">Email address</label> 
          <input type="email" name="email" class="form-control" id="email" /> 
         </div> 
         <div class="form-group"> 
          <label for="phone">Phone</label> 
          <input type="text" name="phone" class="form-control" id="phone" /> 
         </div> 
         <div class="form-group"> 
          <label for="message">Your message</label> 
          <textarea name="message" class="form-control" id="message" rows="6"></textarea> 
         </div> 
         <div class="submit"> 
          <button type="submit" class="button button-small" value="Email us" id="btnContactUs" /> 
         </div> 
</form> 
+0

您的帖子變量不匹配你的名字屬性。因此,請在表單中更正您的姓名屬性。 –

+0

Post是否在我的HTML中使用'for'或'name'或'id'? –

+0

帖子總是使用'name'。 –

回答

0

訪問$ _ POST與輸入的名稱:

/* Check all form inputs using check_input function */ 
$name = check_input($_POST['name'], "Your Name"); 
$email = check_input($_POST['email'], "Your E-mail Address"); 
$subject = check_input($_POST['inputSubject'], "Message Subject"); // subject is not anymore in html 
$message = check_input($_POST['message'], "Your Message"); 
+0

非常感謝。我的意思是交換主題爲'電話'在HTML。 任何其他人蔘考上面的代碼,確保你注意'Subject'不在HTML中,'Phone'不在PHP中。他們的意圖是交換。 –

+0

好的...然後嘗試這個解決方案... –

相關問題