访问域时,表单操作更改为mail.php

时间:2015-01-15 12:19:46

标签: php forms email dns

我使用基本的php从网站邮寄自己 它是一个单页网站,里面有所需的PHP,而不是单独的mail.php。我已经使用了内置验证和必填字段,但是当我访问域而不是domain / index.php时,表单代码会更改以生成mail.php操作,不会进行验证并发送空白电子邮件。

我希望它包含所有必填字段,然后验证然后显示一条小成功消息,让用户知道他们的消息已被发送。 我在下面附上了我的代码。

<?php            
if($_SERVER["REQUEST_METHOD"] == "POST"){
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $message = test_input($_POST["message"]);
   $formcontent="From: $name \n Message: $message";
   $recipient = "me@domian";
   $subject = "Contact Form Subbmission";
   $mailheader = "From: $email \r\n";
   mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
   echo ("Thanks for getting in touch, I'll get back to you as soon as possible.");
}
//clean it
function test_input($data){
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>#contact">
<div class="row 50%">
<div class="6u"><input type="text" name="name" placeholder="Name" required="required"/></div>
<div class="6u"><input type="email" name="email" placeholder="Email" required="required"/></div>
</div>
<div class="row 50%">
<div class="12u"><textarea name="message" placeholder="Message" rows="6" required="required">    </textarea></div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" value="Send Message" /></li>
</ul>
</div>
</div>
</form>

提前感谢您提供的任何帮助

3 个答案:

答案 0 :(得分:1)

试试这个:

<?php
//checks if the post variables are set and also checks that they aren't empty         
if(isset($_POST['name'],$_POST['email'],$_POST['message'])&& $_POST['name'] !="" && $_POST['email'] !="" && $_POST['message'] !=""){
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $message = test_input($_POST["message"]);
   $formcontent="From: $name \n Message: $message";
   $recipient = "me@domian";
   $subject = "Contact Form Subbmission";
   $mailheader = "From: $email \r\n";
   mail($recipient, $subject, $formcontent, $mailheader);
   echo 'Thanks for getting in touch, I\'ll get back to you as soon as possible.';
   }else{
   echo 'You didn\'t fill out all the fields. Please try again';
 }
//clean it
function test_input($data){
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
 }
?>

<form action="index.php" method="post">
<div class="row 50%">
<div class="6u"><input type="text" name="name" placeholder="Name" required="required"/></div>
<div class="6u"><input type="email" name="email" placeholder="Email" required="required"/></div>
</div>
<div class="row 50%">
<div class="12u"><textarea name="message" placeholder="Message" rows="6" required="required"></textarea></div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" value="Send Message" /></li>
</ul>
</div>
</div>
</form>

答案 1 :(得分:0)

在每种情况下都使用更好的错误处理然后&#34;或死&#34;使用邮件功能。

例如

<?php
 if(mail($recipient, $subject, $formcontent, $mailheader)){
     echo ("Thanks for getting in touch, I'll get back to you as soon as possible.");
 }
?>

您的表单不需要此操作。如果它已发布到域本身,请使用以下命令:

<form method="post" action="#contact">

答案 2 :(得分:0)

我刚刚在服务器上创建了一个单独的mail.php文件,这个项目已经超时了。

感谢您的回答

ķ