我正在尝试将php邮件表单设置到我的网站中。首先,我希望在提交后保留表单值。如果用户留下空字段并按下提交按钮,该用户将不得不再次编写相同的内容。我设法用结构<?php if (isset($_POST['Fields'])){echo htmlentities($_POST['Fields']); }?>
来做这件事但是,在提交表单值后,我将表单和php邮件代码相互链接。我还没设法做到这一点。我不了解php。有代码:
我的表格(在contact.php内):
<form method="post" action="mail.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" value="<?php if (isset($_POST['Name'])){echo htmlentities($_POST['Name']); }?>" />
<label for="Subject">Subject:</label>
<input type="text" name="Subject" id="Subject" value="<?php if (isset($_POST['Subject'])){echo htmlentities($_POST['Subject']); }?>"/>
<label for="Phone">Phone:</label>
<input type="text" name="Phone" id="Phone" value="<?php if (isset($_POST['Phone'])){echo htmlentities($_POST['Phone']); }?>"/>
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" value="<?php if (isset($_POST['Email'])){echo htmlentities($_POST['Email']); }?>"/>
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message">
<?php if (isset($_POST['Message'])){echo htmlentities($_POST['Message']); }?>
</textarea>
<input type="submit" name="submit" value="Send" class="submit-button" />
</form>
&#13;
我的php文件(mail.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'];
$subject = $_POST['Subject'];
$phone = $_POST['Phone'];
$visitor_email = $_POST['Email'];
$message = $_POST['Message'];
//Validate first
if(empty($name)||empty($visitor_email)||empty($message))
{
header('Location: contact.php#jump'); // I'm gonna writing new stuff here
exit;
}
if(IsInjected($visitor_email))
{
header('Location: contact.php#jump'); // I'm gonna writing new stuff here
exit;
}
$email_from = 'updatedmail@mail.com';//<== update the email address
$email_subject = "Email from your web site";
$email_body = "You have received a new message via the web mail form \n".
"Name: $name \n".
"E-Mail: $visitor_email \n".
"Subject: $subject \n".
"Message: $message \n".
"Phone Number: $phone \n".
$to = "mymail@mymail.mail";//<== 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: contact.php#jump'); // I'm gonna writing new stuff here
// 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;
}
}
?>
&#13;
答案 0 :(得分:0)
你必须在contact.php中包含mail.php。所以在contact.php中,在顶部写
<?php include("mail.php");?>
如果某个文件夹中的mail.php包含该文件夹名称,则如下所示:
<?php include("foldername/mail.php");?>
从 action =&#34; mail.php&#34;
中删除mail.php如果它不起作用,请告诉我。 :)