Get the value of a select and play in php variable

0

How do I get the selected value from a select and already print it? I have a simple form, when the user select the value I want to assign to a php variable, because I will show the selected quantity and selected amount x the value of the selected ticket.

<div class="col-md-4">
  <div class="form-group">
    <select id="cb_catinsumo" class="form-control   select_ticket" name="teste">
      <option value="0">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
    </select>
  </div>
</div>
    
asked by anonymous 09.12.2017 / 02:44

2 answers

0
  

How do I get the selected value from a select and already print   it?

For this you need a javascript (jQuery is simpler):

$(document).ready( function ()
{
	$("#cb_catinsumo").on('change', function() {
		var option = $(this).find('option:selected').val();
    $('#res').html("Opcao selecionada: "+option);
	});
  });
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script><divclass="col-md-4">
  <div class="form-group">
    <select id="cb_catinsumo" class="form-control   select_ticket" name="teste">
      <option value="0">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
    </select>
  </div>
</div>
<div id='res'></div>

If you want to use with some php snippet, I suggest you do an Ajax. Php is a server side language, that is, it runs on the server and sends the results to the client, since javascript is client side, so it runs on the client side, so to assign or manipulate the values, make an ajax request. p>     

09.12.2017 / 02:58
0

Dude, on top of your doubt I'll try to give you a solution without having to play the PHP variable. The jQuery code for this would be simple:

$(document).ready(function(){
    	
    	$(document).on('click', '#vervalor', function(){
    
    		let valor_ingresso = $('#cb_ingresso option:selected').val();
    		let quantidade_ingresso = $('#cb_catinsumo option:selected').val();
    		let total = (valor_ingresso*quantidade_ingresso);
    
    		$('.total').css('display', 'block');
    		$('#preco_total').html('R$ '+total);
    	});
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divclass="col-md-4">
  <div class="form-group">
    <select id="cb_ingresso" class="form-control   select_ticket" name="teste1">
      <option value="0">Selecione o tipo de ingresso</option>
      <option value="50.00">Inteira</option>
      <option value="25.00">Meia</option>
      <option value="25.00">Lote Promocional</option>
      <option value="100.00">Camarote</option>
    </select>
  </div>
</div>

<div class="col-md-4">
  <div class="form-group">
    <select id="cb_catinsumo" class="form-control   select_ticket" name="teste2">
      <option value="0">Selecione a quantidade de ingresso</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
    </select>
  </div>
</div>

<button type="button" id="vervalor">Visualizar Valor Total</button>

<div class="total" style="display:none">
    Preço Total: <span id="preco_total"></span>
</div>
  

If you need to search the price in the database and do not   select as I did the first, then use jQuery's $ .ajax () function for   example.

    
05.12.2018 / 23:47