PHP variable with a select text (combobox)

1

How to put in a php variable the text and not the value of an option? If it is java script, I still put the text in the variable, since I need to work with that text.

html example:

<select name="uf" id="uf">
	<option value="1">AC</option>
	<option value="2">AL</option>
	<option value="3">AM</option>
  </select>

After submitting, I can only get the value:

$idestado = $_POST['UF'];

How do I put the text in this variable:

$nomeestado = ???
    
asked by anonymous 08.07.2015 / 15:06

1 answer

1

You can keep the join in an array, use it to feed the select, and also get its value later.

<select name="uf">
  <?php

  $estados = array(
    "SP" => "São Paulo",
    "RJ" => "Rio de Janeiro",
  );

  foreach($estados as $k => $v) {
    print('<option value="'.$k.'">'.$v.'</option>'."\n");
  }

  ?>
</select>

after submit:

$uf = $_POST['uf'];
$estado = $estados[$uf];
    
08.07.2015 / 15:48