1
我正在爲我學校的機器人團隊做一個網站。到目前爲止,除了聯繫表格外,一切運行良好。我知道我需要使用PHP來使它工作,並且我在網上找到了一個關於如何去做的指南。我完全複製它,根據需要更改細節。但是,它根本沒有工作。以下是HTML的聯繫人部分。HTML聯繫表單問題?
<!-- Contact -->
<article id="contact">
<h2 class="major">Contact</h2>
<form method="post" action="C:\Users\Mr. Roboto 2\Desktop\Robotics New Website\assets\form-to-email.php">
<div class="field half first">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="4"></textarea>
</div>
<ul class="actions">
<li><input type="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</form>
<ul class="icons">
<li><a href="#" class="icon fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="#" class="icon fa-facebook"><span class="label">Facebook</span></a></li>
<li><a href="#" class="icon fa-instagram"><span class="label">Instagram</span></a></li>
<li><a href="#" class="icon fa-github"><span class="label">GitHub</span></a></li>
</ul>
</article>
以下是我在指南中找到的PHP代碼。
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = '[email protected]esigns.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "[email protected]";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
我是一個PHP的完全新手,所以對我來說沒有任何意義。每當我按下按鈕提交消息時,所發生的只是PHP代碼在空白頁面上顯示。我聽說有些人需要郵件服務器,但我不知道如何設置郵件服務器。我在網上找到的東西都沒有幫助,所以我決定來到老的StackOverflow尋求幫助。希望有人能幫助我。任何幫助將非常感激。
編輯:顯然這被標記爲重複,當我不相信它是。我想重複的問題沒有像我一樣遇到同樣的問題。
如果你看到在瀏覽器中的PHP,你甚至沒有運行一個支持PHP服務器(修復第一) – nogad
'「的PHP代碼顯示了一個空白白頁「 - 然後PHP根本沒有執行。可能是因爲你是從文件系統而不是從web服務器調用它:'action =「C:\ Users \ Roboto 2 \ Desktop \ Robotics New Website \ assets \ form-to-email.php」'PHP在* web服務器*上運行,而不在瀏覽器中運行。 – David