Undefined array key "title" but in only one input

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

Undefined array key "title" but in only one input

问题

<form action="opublikuj/index.php" method="post">
    <div class="title">
        <label for="title">Tytuł</label>
        <input type="text" name="titleInput" id="title"/>
    </div>

    <div class="description">
        <label for="description">Opis</label>
        <textarea
            name="descriptionTxtArea"
            id="description"
            cols="21"
            rows="10"
        ></textarea>
    </div>
</form>

opublikuj/index.php:

<?php
$opis = $_POST['description'];
$tytul = $_POST['title'];
echo $tytul."<br>";
echo $opis."<br>";
?>

在第3行出现了一个警告:Undefined array key "title",但是description正常工作。我尝试在PHP中使用id而不是name,但结果仍然相同。

英文:
&lt;form action=&quot;opublikuj/index.php&quot; method=&quot;post&quot;&gt;
        &lt;div class=&quot;title&quot;&gt;
          &lt;label for=&quot;title&quot;&gt;Tytuł&lt;/label&gt;
          &lt;input type=&quot;text&quot; name=&quot;titleInput&quot; id=&quot;title&quot;/&gt;
        &lt;/div&gt;

        &lt;div class=&quot;description&quot;&gt;
          &lt;label for=&quot;description&quot;&gt;Opis&lt;/label&gt;
          &lt;textarea
            name=&quot;descriptionTxtArea&quot;
            id=&quot;description&quot;
            cols=&quot;21&quot;
            rows=&quot;10&quot;
          &gt;&lt;/textarea&gt;
        &lt;/div&gt;
&lt;/form&gt;

opublikuj/index.php:

&lt;?php
$opis = $_POST[&#39;description&#39;];
$tytul = $_POST[&#39;title&#39;];
echo $tytul.&quot;&lt;br&gt;&quot;;
echo $opis.&quot;&lt;br&gt;&quot;;
?&gt;

it gives me an error:
Warning: Undefined array key "title" on line 3
but description works perfectly. I tried using id instead of name in PHP but it is still the same.

答案1

得分: 1

你必须从 $_POST 中选择 name 属性,而不是 id

尝试:

&lt;?php
$opis = $_POST[&#39;descriptionTxtArea&#39;];
$tytul = $_POST[&#39;titleInput&#39;];
echo $tytul.&quot;&lt;br&gt;&quot;;
echo $opis.&quot;&lt;br&gt;&quot;;
?&gt;
英文:

You have to select th name property from the $_POST, not id.

Try:

&lt;?php
$opis = $_POST[&#39;descriptionTxtArea&#39;];
$tytul = $_POST[&#39;titleInput&#39;];
echo $tytul.&quot;&lt;br&gt;&quot;;
echo $opis.&quot;&lt;br&gt;&quot;;
?&gt;

huangapple
  • 本文由 发表于 2023年8月11日 02:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878432.html
匿名

发表评论

匿名网友

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

确定