How to change URL value, with the information retained in the Select / Option

2

I use this because I'm developing an E-Commercer system, but the client wants the products to be in ComboBox with Select. Until then everything is correct, my difficulty is to make the change to the URL, according to the 'product' selected in <SELECT>/<OPTION>

I need the URL to be changed according to the items selected in Select, this Select makes a query in the database that presents the products in the

I'm getting the information when I choose the given SELECT OPTION: My problem is changing the URL.

Follow the Code.

// I get the OPTION values and change to a variable.

echo "<script language='javascript'>
     function PegarValores(){
    pd_nome = document.getElementById('produto_nome').options[document.getElementById('produto_nome').selectedIndex].text;
    pd_id = document.getElementById('produto_nome').value;
    alert(pd_id+' - '+pd_nome);
      }
   </script>";

Alert is correctly retrieving the values of the selected select.

The problem now comes up, how to make the URL automatically change according to the value selected in the select.

echo "<select id='produto_nome' name='produto_nome[]' value='' onchange='PegarValores()'>
<option value='-1'>
    Selecione
</option>";

while ($proteina = mysqli_fetch_assoc($query)){
    extract($proteina);
    echo '<option value="' . $produto_id . '">' . $produto_nome . '</option>';
}
echo "</select>";
echo "<a href='addtocart.php?id='$produto_id&name=$produto_nome' class='customButton'>";
echo "Adicionar";
echo "</a>"; 
    
asked by anonymous 12.05.2014 / 15:29

1 answer

2

See this example here using jQuery: link

HTML:

<select id='meu_select'>
  <option value='1'>Valor 1</option>
  <option value='2'>Valor 2</option>
</select>

Javascript:

// pega instância do select
var meu_select = $('#meu_select');
// adiciona evento quando o usuário selecionar outro valor
meu_select.change(function() {
  // pega valor do option selecionado
  var valor = meu_select.val()
  alert('Seu valor é: ' + valor);
  // muda url do navegador
  location.href = '#?param1=' + valor;
})
    
12.05.2014 / 17:56