【问题标题】:Get select multiple values in PHP在PHP中获取选择多个值
【发布时间】:2017-05-13 13:31:37
【问题描述】:

我尝试以 html 形式获取多项选择的值。

我在我的“名称”属性中添加了括号,但它仍然不起作用。

我在课程中使用了一个“框架”,有完整的例子:https://github.com/bpesquet/MonBlog/

表格

<form method="post" action="index.php?controleur=Creer&action=addFilm" style="display:none" id="addFilm">

  <label>Title</label>
  <input type="text" name="title" required> <br>

  <label>Release date</label>
  <input type="date" name="release" required> <br>

  <label>Synopsis</label>
  <textarea name="synopsis"></textarea> <br>

  <label>Nationality</label>
  <select name="nationality[]" multiple>
    <?php foreach($pays as $payss): ?>
      <option><?= $payss['nom_pays'] ?></option>
    <?php endforeach ?>
  </select> <br>

  <label>Genre</label>
  <select name="genre[]" multiple>
    <?php foreach($genres as $genre): ?>
      <option><?= $genre['nom_genre']  ?></option>
    <?php endforeach ?>
  </select> <br>

  <label>File name</label>
  <input type="text" name="filename" required> <br>

  <input type="submit" value="Submit">
</form>

我用数据库中的 php 填充 select 元素。

我的 $_POST 变量是

Array ( [title] =&gt; test_film [release] =&gt; 2000-11-20 [synopsis] =&gt; [nationality] =&gt; USA [genre] =&gt; Comedy [filename] =&gt; test )

(只选择最后一个元素作为字符串)

当我在导航器中查找请求时,我有正确的 POST 参数 它看起来像这样:

title: "test_film"
release: "2000-11-11"
synopsis: ""
nationality: [2]
    [0]: "USA"
    [1]: "France"
genre: [2]
    [0]: "Thriller"
    [1]: "Comedy"
filename: "test"

但变量 'genre' 不是 $_POST 中的数组

【问题讨论】:

  • 制作 print_r($_POST) 并向我们展示。
  • 尝试将value 属性添加到您的选项标签
  • 我真的不明白。正如@noctilux 昨天所说,我添加了“价值”属性,它可以工作。我今天删除它,它仍然有效......

标签: php html


【解决方案1】:

您的代码一切正常,我认为您打印变量的方式不正确: 试试这个:

echo '<pre>';
print_r($_POST);die;

【讨论】:

    猜你喜欢
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多