Taking values of equal variables by URL

Asked

Viewed 540 times

0

I got a little complicated in the title to explain my doubt, but I will try to be as clear as possible.

I have this field:

Meu campo

I need to get the values that were selected in the extension field, but when I get his result via POST he only brings a value and via GET too, but he brings it in the URL:

https://192.168.0.27/projetos/bdm/grupo_de_permicao.php?formfield1=teste&my_multi_select1=200&my_multi_select1=4864&my_multi_select1=2000

I was wondering if you can get all these values with the same variable my_multi_select1 and play within an array or some other way.

I don’t think it’s much use, but just follow my code:

<div class="form-group">
                               <label class="form-label" for="formfield1">Ramais</label>
                                    <span class="desc">Adicione ou remova os ramais utilizando a caixa abaixo</span>
                                    <select name="my_multi_select1" class="multi-select form-control" multiple="" id="my_multi_select1" name="formfield2">

                                        <?php
                                $allramal = "";
                                $tst2 = selectRamals();
                                while ($linha = mysqli_fetch_array($tst2)) {
                                  ?>
                                    <option value="<?=$linha['extension']?>"><?=$linha['name']?>   (<?=$linha['extension']?>)</option>
                                <?php } ?>
                                    </select>      
                                </div>

2 answers

2


I recommend via POST, because as we all know, GET has a character limit that depends on the browser, and if it is something that the client can make many selections, maybe it gives problem via GET.

Do so (I made an example for your business, but without using Bootstrap, to serve as a model for other people and make the code simpler, just re-adapt).

<form action="" method="post">
<select name="my_multi_select1[]" multiple="multiple">
    <?php
        $allramal = "";
        $tst2 = selectRamals();
        while ($linha = mysqli_fetch_array($tst2)) {
            echo "<option value='{$linha['extension']}'>{$linha['name']} ({$linha['extension']})</option>";
        }
    ?>
</select>

Note that in the name of the input, we put a [] to indicate to PHP that the variable that will receive the values of this field, must be an array.

To print on the screen, do:

print_r($_POST['my_multi_select1']);

Obs.: when you are rewriting the code, arrange the name of the input where you have the options, you set the name property twice in the same field.

  • Thanks!! I won’t be able to answer you now but tomorrow I will test and comment here!! Valeuzao

  • That’s right. The [] will create a array of my_multi_select1. To access each one can use the foreach($_POST['my_multi_select1'] as $multi){ echo $multi; }

  • Thanks!! Absolutely right

0

I recommend using $_POST as it is safer and there are no limitations like $_GET

And you can manipulate these results with Foreach Foreach

foreach($_POST['my_multi_select1'] as $value)
{ 

   echo $value; 


}

Browser other questions tagged

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