How to take selected value from Dropdown Menu and insert into a PHP variable?

Asked

Viewed 36 times

-3

I have the following dropdown menus on my website:

<div class="dropdown">
          <button class="btn btn-secondary dropdown-toggle col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 col-xxl-12" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
            Selecione o Campo
          </button>
          <ul class="dropdown-menu col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 col-xxl-12" aria-labelledby="dropdownMenuButton1">

            <li><a class="dropdown-item">Codigo</a></li>
            <li><a class="dropdown-item">Cargo</a></li>
            <li><a class="dropdown-item">Tipo</a></li>
            <li><a class="dropdown-item">Nome</a></li>
            <li><a class="dropdown-item">Nome Usual</a></li>
            <li><a class="dropdown-item">Endereco</a></li>
            <li><a class="dropdown-item">Numero</a></li>
            <li><a class="dropdown-item">Complemento</a></li>

   </ul>
 </div>

Tipo de Busca:
    
    <div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 col-xxl-12" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
        Selecione a Busca
      </button>
      <ul class="dropdown-menu col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 col-xxl-12" aria-labelledby="dropdownMenuButton1">
        <li><a class="dropdown-item" >Comeca com</a></li>
        <li><a class="dropdown-item" >Igual a</a></li>
        <li><a class="dropdown-item" >Termina com</a></li>
        <li><a class="dropdown-item" >Contem</a></li>
        <li><a class="dropdown-item" >Contem as partes</a></li>
      </ul>
    </div>

<div class="row">
            <div class="col-6 col-sm-6 col-md-6 col-lg-6 col-xl-6 col-xxl-6"> </div>
            <div class="col-6 col-sm-6 col-md-6 col-lg-6 col-xl-6 col-xxl-6"> <button type="button" class="btn btn-primary col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 col-xxl-12">Buscar</button> </div>
 </div>

I need to take what the user selects in the Dropdown Menus and insert into a PHP variable to make a query in the database through a SELECT. How can I do that?

  • 1

    Please edit the question to limit it to a specific problem with sufficient detail to identify an appropriate answer.

  • Oh my God. I don’t know how to make it more specific than that.

  • 1

    You have to submit an AJAX request to the back end with the dropdown value.

2 answers

-2

First I would use a select to take the user’s choice and put it into a form, this way:

Select the Search

<form action="/test2" method="post">
    @csrf
    <select name="select_data" class="form-select" aria-label="Default select example">
        <option selected>Open this select menu</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
    <button type="submit">Enviar</button>
</form> 

Note the name attribute, which will be passed with the chosen value:

To test the "pass", I made a POST route that gives a dd in the request:

Route::post('/test2', function () {
    dd(request()->all());
});

As a way out we have:

array:2 [▼
  "_token" => "6IIS7TBpFYwRIRkDXM9N2LKsFKdaiFvhgEe2G2pV"
  "select_data" => "2"
]
  • This is better than the other answers(1 has been deleted). More still remains the question and if the AP is not using the Laravel Framework?

-4

Browser other questions tagged

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