0

我相信我的代码是正确的,当我删除 isset 语句时,它会返回错误,就好像未设置字段一样。我听说这可能是由于服务器上的 PHP 配置方式可能是这种情况。似乎变量没有从 HTML 传递到 PHP 代码。如果这意味着什么,我已经使用 WebPlatformInstaller 安装了我的 PHP,并在 Windows Server 2016 上运行 IIS

<?php
echo "test";

    if(isset($_POST['email'])) {
        echo "success";
        $email_to = "temp@gmail.com";
        $email_subject = "Your email subject line";

        function died($error) {
            // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }


        // validation expected data exists
        if(!isset($_POST['email']) ||
            !isset($_POST['message'])) {
            died('You must enter your email and a message');       
        }


        $email_from = $_POST['email']; // required
        $message = $_POST['message']; // required

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

        if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }

        $string_exp = "/^[A-Za-z .'-]+$/";

        if(strlen($message) < 2) {
        $error_message .= 'The message you entered do not appear to be valid.<br />';
        }

        if(strlen($error_message) > 0) {
        died($error_message);
        }

        $email_message = "Form details below.\n\n";


        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "message: ".clean_string($message)."\n";

        // create email headers
        $headers = 'From: '.$email_from."\r\n".
        'Reply-To: '.$email_from."\r\n" .
        'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers);  
        ?>

        <!-- include your own success html here -->

        echo "Thank you for reaching out! I'll be in touch.";

        <?php

        }
        ?>`

HTML

<form action="contact.php" method="post" enctype="text/plain">
      <input type="email" placeholder="Email" name='email' required>
      <input class="btn" type='submit' tabindex="1" value='Send'>
      <textarea type='text' name='message' required></textarea>
    </form>
4

2 回答 2

3

阅读:错误#33741 $_POST superglobal 未填充 ,这与您的问题有关。

并在那里回答:

标记中 enctype 的有效值为:

应用程序/x-www-form-urlencoded

多部分/表单数据


使用代码示例检查这些屏幕截图以查看结果:

文本/纯文本

文本/纯文本

应用程序/x-www-form-urlencoded

urlencoded



额外:php://输入流

php://输入流



作为请求主体,它只改变@符号(这就是它的urlencoded的原因)

请求编辑

结果:这不是 PHP 的错误,它是所有 PHP 版本接受支持的 enctypes 的共同特性,它只是在“看到”不同的内容类型时不填充$_POST数组。php://input但是,如果您坚持,您可以从php://input流中读取它。

链接到手册

于 2018-10-01T22:44:38.617 回答
0

为什么你使用 enctype="text/plain" ?

HTML 表单提供了三种编码方法:

application/x-www-form-urlencoded (the default)
multipart/form-data
text/plain   (Never use)

当您编写客户端代码时,您只需要知道当您的表单包含任何<input type="">元素时使用 multipart/form-data。

于 2018-10-01T22:41:23.803 回答