我正在尝试为我校的机器人团队建立一个网站。到目前为止,一切都运作良好,除了联系表格。我知道我需要使用PHP来使其工作,我在网上找到了如何做到这一点的指南。我完全复制了它,根据需要更改了细节。但是,它根本不起作用。下面是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 = 'tom@amazing-designs.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 = "FRCteam3236@gmail.com";//<== 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寻求帮助。希望有人可以帮助我。任何帮助将不胜感激。
编辑:显然,当我不相信时,这被标记为重复。我认为重复的问题并没有像我一样遇到同样的问题。
答案 0 :(得分:-1)
看起来PHP没有执行。 PHP需要在服务器上运行。您应该安装WAMP或XAMPP并在那里运行PHP脚本。