简体   繁体   English

从带有 php 没有 jquery 的 select2 中获取值

[英]Get values ​from select2 with php without jquery

I have a registration system where there are several fields.我有一个注册系统,其中有几个字段。 One of the registration fields is Segments and they come from the database:注册字段之一是 Segments,它们来自数据库:

public function comboSegmentos($key)
{
    $sqlMostrar = mysqli_query($this->conexao, "SELECT * FROM loja_segmentos;");
    if (mysqli_num_rows($sqlMostrar) == 0) 
    {
        $mostrar = "<div style='color: red'>Nenhuma segmento cadastrado até o momento.</div>";
    } 
        else 
    {
        $mostrar = "<select name='Segments[]' id='segmentos' class='form-control select2' multiple='multiple' style='width: 35%'>";
        while ($jmMostrar = mysqli_fetch_object($sqlMostrar)) 
        {
                $mostrar .= "<option value='".$jmMostrar->Segmentos."' ".$selected.">".$jmMostrar->Segmentos."</option>";
        }
        $mostrar .= "</select>";
    }
    return $mostrar;
}

So that the method doesn't have too many parameters, I did it like this:为了使该方法没有太多参数,我这样做了:

<?php
...
$dados = array_filter($_POST);
echo $metodos->cadastrarProdutos($dados);

Method cadastrarProdutos($dados)方法 cadastrarProdutos($dados)

But I'm not able to get the values of the Segments even using foreach().但即使使用 foreach(),我也无法获得段的值。

<?php
public function cadastrarProdutos(array $dados)
{
  ...
  $segments = $dados["Segments"];
  foreach($segments as $seg)
  {
     $segm = $seg;
  }
  ...
}

How can I get the values of the select2 field and get them using a PHP method without Jquery?如何获取 select2 字段的值并使用没有 Jquery 的 PHP 方法获取它们?

I managed to solve it as follows:我设法解决它如下:

<?php
public function cadastrarProdutos(array $dados)
{
  ...
  $segments = $dados["Segments"];
  $segm = array();
  foreach($segments as $seg)
  {
     $segm[] = $seg;
  }
  $segments = implode(',',$segm);
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM