Take the value of an html select option and put it in the query.

Asked

Viewed 423 times

1

I am with a huge doubt, it would be the following;

I need to take the value of the Select Option that is in the HTML form and play in the SQL query that is in the same file . PHP, follow ex:

    <select name="origem" id="origem">
  <option>-- Selecione a Origem --</option>
  <?php
                $Q_secc = "SELECT codigo,descricao FROM seccionamentos ORDER BY descricao";
                $R_secc = mysql_query($Q_secc,$conexao);
                while ($l_secc = mysql_fetch_array($R_secc)){
                ?>
  <option value="<?=$l_secc['codigo']?>">
  <?=utf8_encode($l_secc['descricao']);?>
  </option>
  <?php } ?>
</select>

this is already being fed by the select above. and below is another select option, only this would have to be powered by the value selected above.

<select name="horario" id="horario">
  <option>-- Selecione a Horário --</option>
  <?php
                $Q_horario = "SELECT H.codigo, H.horario
                           FROM tarifas T
                           INNER JOIN linhas L ON L.codigo = T.linha
                           INNER JOIN horarios H ON H.linha = L.codigo
                           WHERE T.origem = valor do 1º combo
                           AND T.destino= 170
                           AND H.sentido = 'I'
                           AND ((MID(H.frequencia, 1, 1) = 'S') AND 
                           (MID(H.frequencia1002, 1, 1) = 'S'))
                           ORDER BY H.horario
                ";
                $R_secc = mysql_query($Q_horario,$conexao);
                while ($l_secc = mysql_fetch_array($R_secc)){
                ?>
  <option value="<?=$l_secc['codigo']?>">
  <?=utf8_encode($l_secc['horario'])?>
  </option>
  <?php } ?>
</select>

I would like Where to be the value of the first source combobox.

  • From what I understand you will need to make an asynchronous request where you will call a page php just to bring the options of the second select, you get something from ajax, javascript? Here is an example link that can help you: https://www.codexworld.com/dynamic-dependent-select-box-using-jquery-ajax-php/

  • I wouldn’t, because I have to do it on the same form!

  • I wouldn’t, because I have to make that call on the combo on the same form.

  • For the user will be in the same form, I suggest reading about ajax.

  • There is no way to do this. How will PHP know which option was selected? Only using Ajax or re-loading the page.

No answers

Browser other questions tagged

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