How to use multiselect value in php

Asked

Viewed 124 times

3

I’m taking a value from a multiselect and sending it via ajax:

Assuming the value of multiselect is:

["1","2","3"]  // é esse valor que está sendo enviado (value do multiselect)

After rescuing this value in the php file you received from ajax, how could I blow up that value just to pick up the numbers?

  • foreach for an array or this ai is a string?

  • 1

    Have you tested $arr = json_decode('["1","2","3"]'); how do you want to use these numbers? note that these numbers are strings...

  • So rray, I’m using a multiselect and when I do: $("#multiselect"). val() it returns it to me. In this case there are several values selected at once and sent via ajax. I would like to separate these values to use it in a foreach

  • Yes I managed to make it work here, but how can I get it according to the amount sent? Because putting the values manually there worked, but the values will be sent via ajax. I get this value like this: $valor = $_POST['valor'], and then have to do it

  • 1

    forehead with $arr = json_decode($valor);. That’s how it works? Otherwise var_dump($valor);?

  • then, it gave the error Invalid argument supplied for foreach() in C: xampp htdocs Matriculas index322.php on line 55 and var_dump is null

  • 1

    Remember to pass true as json_decode’s second argument otherwise it returns an object

Show 2 more comments

3 answers

1

If this value is a string, replace the unwanted characters with the valid ones and explode by comma this will transform the string into an array.

<?php

$valor = '"["1","2","3"]"';
$str = str_replace(array('"','[', ']'), '', $valor);
$itens = explode(',', $str);

echo '<pre>';
print_r($itens);
  • rray, excuse me man, the right amount is as follows ["1","2","3"], without those double quotes involving the value

  • @Diegodesouzasilva, json_decode() is the best option then, as recommended by Sergio

1


Well, after doing some tests I found that it had a value that was receiving wrong quote.

Sergio’s comment helped a lot, so

$arr = json_decode('["1","2","3"]');

is valid. In my case, I was able to get the array directly the way it was coming from ajax as well

then:

$array = $_POST['array'] // recebida via ajax

foreach($array as $valor){
      $query = "INSERT INTO table (col1, col2) VALUES ('$valor', '1')"; 
      $resultado = mysqli_query($conexao->abrirConexao(), $query);
    }

also worked properly!

What counts is to test, to break the head, that one hour it ends up getting!

1

The correct answer depends on how you sent the data via ajax, whether it was through the POST, GET or PUT method... But assuming that you sent via POST, the data received is apparently an array, in this case it would be something similar to this:

/**
   'data' é a variável que você
   definiu no método do ajax,
   se vc não definiu, basta
   capturar $dados = $_POST;
**/ 
   $dados = $_POST['data'];

  $numeroUm   = $dados[0];
  $numeroDois = $dados[1];
  $numeroTres = $dados[2];

 echo $numeroUm.'<br>' .
      $numeroDois.'<br>' .
      $numeroTres.'<br>';
      die();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.