dqalnwuci494308 2018-10-01 22:16
浏览 140
已采纳

当表单具有enctype =“text / plain”时,无法从$ _POST数组中获取字段

I believe that my code is correct, when I remove the isset statement it returns an error as if the fields were not set. I have heard that this may be due to how PHP is configured on the server which may be the case. It seems like the variables aren't being passed from the HTML to the PHP code. If it means anything I have my PHP installed with WebPlatformInstaller and running IIS on windows server 2016

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

";


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

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

        // create email headers
        $headers = 'From: '.$email_from."
".
        'Reply-To: '.$email_from."
" .
        '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>
  • 写回答

2条回答 默认 最新

  • duanbi1888 2018-10-01 22:44
    关注

    Read this: Bug #33741 $_POST superglobal not populated which is about Your issue.

    and answer there:

    Valid values for enctype in tag are:

    application/x-www-form-urlencoded

    multipart/form-data


    Check these screenshots with code samples to see results:

    text/plain

    text/plain

    application/x-www-form-urlencoded

    urlencoded



    EXTRA: php://input stream

    php://input stream



    as request body it does not change anything but @ symbol (that's why it's urlencoded)

    request edit

    RESULT: It's not a bug of PHP, it's common feature for all PHP versions to accept supported enctypes, it just does not populate $_POST array from php://input when it "see" different content-type. But if You insist You can read it from php://input stream.

    link to manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c