为了账号安全,请及时绑定邮箱和手机立即绑定

我无法在 php 中获取选择框的多个选定值

我无法在 php 中获取选择框的多个选定值

PHP
慕姐4208626 2023-04-15 17:02:35
因此,我有一个 HTML/PHP 表单,它有一个选择列表框,您可以从中选择多个值,因为它的多个属性设置为多个。考虑表单方法是“POST”。但是当我点击 SUBMIT 时,列表框只保存了一个值(具体来说是最后选择的值)。我不知道为什么。这是我的代码的一部分:    <?php    if(isset($_POST['submitSave'])) {    // Disable errors due to empty xml files        error_reporting(E_ALL & ~E_WARNING);        $domDoc = new DOMDocument('1.0', 'UTF-8');        $domDoc->preserveWhiteSpace = false;        $domDoc->formatOutput = true;        $domDoc->encoding = 'UTF-8';        $domDoc->load('./data/expression.xml');        $xpath = new DOMXpath($domDoc);        if($domDoc->getElementsByTagName('expression')->length>0){            // If we already have expression tag defined            $expression = $domDoc->getElementsByTagName('expression')[0];        }else{            // If we don't have any expression tag, i.e. file is empty            $expression = $domDoc->createElement('expression');        }    $vocabulario = $domDoc->createElement('vocabulario');    $vocabulario->setAttribute('word', $_POST['word']);    $classe = $domDoc->createElement('classe', $_POST['classe']);    $domDoc->appendChild($expression);    $expression->appendChild($vocabulario);    $vocabulario->appendChild($classe);    file_put_contents('./data/expression.xml', $domDoc->saveXML());    header('location:index.php');}?><form method="post"><div class="col-75">  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>  <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet" />  <select name="classe[]" multiple="multiple">    <option value="| adjective |">adjective</option>    <option value="| adverb |">adverb</option>    <option value="| noun |">noun</option>    <option value="| verb |">verb</option>  </select>  <script type="text/javascript">    var s2 = $("#classe").select2({      placeholder: "Select",      tags: true    });  </script></div>
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

该代码适合我:


/home/vagrant/foo/index.php:3:

array (size=2)

  'classe' => 

    array (size=2)

      0 => string '| adjective |' (length=13)

      1 => string '| adverb |' (length=10)

  'submitSave' => string 'Save' (length=4)

在 PHP 中有一些特殊的配置?


我的测试:


<?php


var_dump($_POST);


?>

<form method="POST">

     <div class="col-75">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet"/>

    <select id="classe" name="classe[]" multiple name="event_type[]" class="form-control" required>

  <option value="| adjective |">adjective</option>

  <option value="| adverb |">adverb</option>

  <option value="| noun |">noun</option>

  <option value="| verb |">verb</option>

</select>

<script type="text/javascript">

        var s2 = $("#classe").select2({

    placeholder: "Select",

    tags: true

});



    </script>

</div>

<div class="row">


            <td><input type="submit" value="Save" name="submitSave"></td>

    </div>  

</form>

编辑:


当你在 PHP 中接收数组数据时,在你的情况下:


<select name="classes[]" >

PHP var$_POST['classes']不是$_POST['classes[]'],它是一个数组,如果你想将它用作字符串,你必须使用implode


查看完整回答
反对 回复 2023-04-15
  • 1 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信