HTML表单的POST方法转换为GET方法

huangapple go评论62阅读模式
英文:

HTML form POST method transformed into GET method

问题

我有一个非常神秘的问题,导致每个<form method="POST">在单个文件上都无法正常工作,由于某种原因,POST方法被转换为GET方法,使表单元素无效。我不知道是否被授权,但我制作了一个关于这个“问题”的视频这里,因为没有人真正理解我在说什么。

如果您有关于为什么在测试文件夹上可以工作,但在管理文件夹上不工作的潜在想法,即使两个代码都相同,请告诉我。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="POST" action="php/SetSanction.php">
  <div>
    <div>
      <div>
        <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px;">
          <div class="form-group">
            <label for="daySelect">Day:</label>
            <select class="form-control" name="daySelect" id="daySelect">
              <option value="0">0</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
              <option value="6">6</option>
              <option value="7">7</option>
              <option value="8">8</option>
              <option value="9">9</option>
              <option value="10">10</option>
              <option value="11">11</option>
              <option value="12">12</option>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              <option value="16">16</option>
              <option value="17">17</option>
              <option value="18">18</option>
              <option value="19">19</option>
              <option value="20">20</option>
              <option value="21">21</option>
              <option value="22">22</option>
              <option value="23">23</option>
              <option value="24">24</option>
              <option value="25">25</option>
              <option value="26">26</option>
              <option value="27">27</option>
              <option value="28">28</option>
              <option value="29">29</option>
              <option value="30">30</option>
            </select>
          </div>
          <div class="form-group">
            <label for="monthSelect">Month:</label>
            <select class="form-control" name="monthSelect" id="monthSelect">
              <option value="0">0</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
              <option value="6">6</option>
              <option value="7">7</option>
              <option value="8">8</option>
              <option value="9">9</option>
              <option value="10">10</option>
              <option value="11">11</option>
              <option value="12">12</option>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              <option value="16">16</option>
              <option value="17">17</option>
              <option value="18">18</option>
              <option value="19">19</option>
              <option value="20">20</option>
              <option value="21">21</option&gt

<details>
<summary>英文:</summary>

I have a very mysterious problem make every `&lt;form method=&quot;POST&quot;&gt;` not working on a single file, for x reason the POST method get transformed into a GET method making the form element useless. I don&#39;t know if it&#39;s authorized but i make a video about this &quot;problem&quot; [here][1] since no one really understand what i&#39;m meanning.

If you have potential idea why it is working on the test folder but not on the admin folder even if both code are the same please let me know it.


  [1]: https://streamable.com/7pggj6

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="POST" action="php/SetSanction.php">
<div>
<div>
<div>
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px;">
<div class="form-group">
<label for="daySelect">Day:</label>
<select class="form-control" name="daySelect" id="daySelect">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
</select>
</div>
<div class="form-group">
<label for="monthSelect">Month:</label>
<select class="form-control" name="monthSelect" id="monthSelect">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
</select>
</div>
<div class="form-group">
<label for="yearSelect">Year:</label>
<select class="form-control" name="yearSelect" id="yearSelect">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
</select>
</div>
</div>
</div>

  &lt;div&gt;
&lt;label&gt;Reason&lt;/label&gt;
&lt;textarea name=&quot;reasonInput&quot; id=&quot;reasonInput&quot; class=&quot;form-control&quot; placeholder=&quot;...&quot; rows=&quot;3&quot; maxlength=&quot;400&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal-footer&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Set&quot;&gt;
&lt;/div&gt;

</div>
</form>
</body>
</html>


And the PHP File (SetSanction.php)

<?php
print_r($_POST); // THIS ARRAY SHOULD BE EMPTY BECAUSE POST NOT WORKING

if ($_SERVER['REQUEST_METHOD'] === 'POST') /NOT WORK BECAUSE IT'S NOT POST REQ/ {
$daySelected = $_POST['daySelect'];
$monthSelected = $_POST['monthSelect'];
$yearSelected = $_POST['yearSelect'];
$reasonInput = $_POST['reasonInput'];

echo &quot;Day: &quot; . $daySelected . &quot;&lt;br&gt;&quot;;

}


</details>
# 答案1
**得分**: 1
如果您正在重写URL以删除扩展名,这将导致表单失效,因为表单操作不会是有效的URL。
最佳选项是在`.htaccess`文件中排除表单操作URL/文件。在您的情况下,可以采取类似以下方式:
`RewriteCond %{REQUEST_URI} !^/php/SetSanction\.php$`
<details>
<summary>英文:</summary>
If you are rewriting the urls to remove the extensions, this will break the form because the form action won&#39;t be a valid url.
Best option would be to exclude the form action url / file from the `.htaccess` file.
Something like this in your case:
`RewriteCond %{REQUEST_URI} !^/php/SetSanction\.php$ ` 
</details>

huangapple
  • 本文由 发表于 2023年2月14日 04:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定