【问题标题】:form action changes to mail.php when visiting domain访问域时表单操作更改为 mail.php
【发布时间】:2015-01-15 12:19:45
【问题描述】:

我正在使用一个基本的 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>

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

【问题讨论】:

    标签: php forms email dns


    【解决方案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>
    

    【讨论】:

    • 作为示例,我将表单操作设置为index.php。您应该将其设置为所有这些代码所在的文件名。
    【解决方案2】:

    在任何情况下,都使用更好的错误处理,然后使用邮件功能“或死”。

    例如;

    <?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">
    

    【讨论】:

    • 该操作仍然在浏览器中将自身更改为 mail.php 并忽略所有验证
    • 服务器上是否有mail.php文件?这是您在服务器上的所有代码吗?
    • 在这个文件中,它是唯一的 php.ini 文件。没有 mail.php 文件。
    • 如果服务器上没有 mail.php 文件并且这是您的代码,则几乎不可能发送电子邮件。你能创建一个phpfiddle吗? codepad.viper-7.com
    【解决方案3】:

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

    感谢您的回答

    K

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 2011-11-20
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多