PHP variable with select(combobox) text

Asked

Viewed 1,017 times

1

How to put the text in a php variable and not the value of an option? If it is java script, still as I put the text in the variable, because I need to work with this 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 Submit I can only get the value:

$idestado = $_POST['UF'];

how I put the text in this variable:

$nomeestado = ???
  • Where does this HTML come from? Because you have numbers in value and not the same as .innerHTML? If you don’t need value in Javascript it’s best to have the same in both... or take value.

  • I use numbers in value because I select cities by state id.

  • Okay, either you use AJAX or I suggest you change what you have in value to use in JS and server. Otherwise form Submit only sends value...

  • ok, so how do I do in AJAX?

  • @Ályssonalexandre you are using js to pick the right cities?

  • ------------yes.

Show 1 more comment

1 answer

1


You can keep the association in an array, use it to power the select, and also to get its value after.

<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 the Submit:

$uf = $_POST['uf'];
$estado = $estados[$uf];

Browser other questions tagged

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