Take the value of a select and play in php variable

Asked

Viewed 2,337 times

1

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

<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>

2 answers

1

Face, above your doubt I will try to give you a solution without having to play in 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>
<div class="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>

In case you need to search the price in the database and not do the select how I did the first one, then use jQuery’s $.ajax() function by example.

1

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

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>
<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>
<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, while javascript is client side, so it runs on the client side, so to assign or manipulate the values, make an ajax request.

  • Thanks for the help, there’s some link that shows this kind of request for me to have a basis on how to do this?

  • You can find several examples here: https://answall.com/search?q=requisi%C3%A7%C3%A3o+ajax or even reading in the jQuery documentation, or w3school https://www.w3schools.com/js_ajax_examples.asp

Browser other questions tagged

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