首页 > 解决方案 > 并非所有在 POST 中传递的变量

问题描述

我正在开发一个(据说)简单的页面和脚本来发送带有附件的电子邮件,但是遇到了一个问题,即最后一个值没有在 POST 中发送。如果我能弄清楚,那就危险了。

发送页面的 HTML 是:

    <form action="contact_reply.php" method="post" name="form1"  required="required" id="form1" enctype="multipart/form-data" >
    <p>
    <input type="hidden" name="PageURL" value="<?php echo GetSelfURL() ?>" />
  </p>
    <p>Your Name<br />
    <span id="sprytextfield1">
    <input name="MSG_Name" type="text"  required="required" id="MSG_Name" tabindex="1" size="70" maxlength="70" />
</span>
</p>

    <p>Your Email Address<br />
    <span id="sprytextfield2">
    <input name="MSG_Email" type="text"  required="required" id="MSG_Email" tabindex="2" size="70" maxlength="70" />
    </span>
    </p>

    <p>Subject<br />
    <span id="sprytextfield3">
    <input name="MSG_Subject" type="text"  required="required" id="MSG_Subject" tabindex="3" size="70" maxlength="70" />
    </span>
    </p>

    <p>Message<br />
    <span id="sprytextarea1">
    <textarea name="MSG_Text"  required="required" id="MSG_Text" cols="70" rows="5" tabindex="4"></textarea>
    </span>
</p>

    <?php
if ($Allow_Attachments)
{
?>            
    <p>Attachment (images only):
    <input name="MSG_Attachment" type="file" id="MSG_Attachment" tabindex="5" accept="image/*" multiple >
</p>
<?php
}
?> 
  <p align="center">
    <input 
    type="submit" 
  name="Contact_SendBtn" 
  id="Contact_SendBtn" 
  value="Submit" 
  tabindex="5"
  vakue="send"
    style="width:60px"/>
   </p>
</form>

接收端的php是:

    function CheckMessageInfo()
{
global $MSG_Name, $MSG_Email, $MSG_Subject, $MSG_Text, $MSG_Attachment, $MSG_Error;

$MSG_Error = '';

echo "<p>".var_dump($_POST)."</p>"; 

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
if (empty($_POST["MSG_Name"])) 
    {
  $MSG_Error .= "A name is required. ".PHP_EOL;
  } 
    else
    {
  $MSG_Name = test_input($_POST["MSG_Name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$MSG_Name)) 
        {
    $MSG_Error .= "Only letters and spaces are allowed in the name. ".PHP_EOL;
    }
    }

  if (empty($_POST["MSG_Email"])) 
    {
        $MSG_Error .= "An email address is required. ".PHP_EOL;
    }
    else
    {
    $MSG_Email = test_input($_POST["MSG_Email"]);
    if (!filter_var($MSG_Email, FILTER_VALIDATE_EMAIL)) 
        {
    $MSG_Error .= "Invalid email format. ".PHP_EOL;
    }
}

  if (empty($_POST["MSG_Subject"])) 
    {
        $MSG_Error .= "A subject for the message is required. ".PHP_EOL;
    }
    else
    {
        $MSG_Subject = filter_var($_POST["MSG_Subject"],FILTER_SANITIZE_STRING);
}

  if (empty($_POST["MSG_Text"])) 
    {
        $MSG_Error .= "The message's text is required. ".PHP_EOL;
    }
    else
    {
        $MSG_Text = filter_var($_POST["MSG_Text"],FILTER_SANITIZE_STRING);
}

    if (empty($_POST["MSG_Attachment"]))
    {
        $Allow_Attachments = FALSE;
    }
    else
    {
        $MSG_Attachment = $_POST["MSG_Attachment"];
    }
    return TRUE;
}
else
{
    return FALSE;
}
}

var_Dump 行给了我这个:

array(6) {
  ["PageURL"]=> string(39) "www.vintagebankantiques.net/contact.php" 
  ["MSG_Name"]=> string(4) "Mike"
  ["MSG_Email"]=> string(21) "me@myemail.com" 
  ["MSG_Subject"]=> string(4) "test"
  ["MSG_Text"]=> string(21) "improved echo $_POST."
  ["Contact_SendBtn"]=> string(6) "Submit"
}

未列出“MSG_Attachment”变量。它去哪儿了?

标签: phpfilepost

解决方案


您可以通过以下方式获取 MSG_Attachment 的值

$_FILES['MSG_Attachment']["tmp_name"]

//if MSG_Attachment is array type
$_FILES['MSG_Attachment']["tmp_name"][0]
$_FILES['MSG_Attachment']["tmp_name"][1] 

由于 MSG_Attachment 输入类型是文件,因此您无法进入 POST。

你也可以参考这个链接


推荐阅读