如何从动态选择框中检索选定的值

I would like to know how to retrieve a selected value from a dynamically selection box. If I get the selected value then I will store it into another variable that is located in another php file. This variable will help me in a sql query in postgresql.

//First php file

 <form name="boton_alta_soniador" action="dar_alta_soniador.php" method="POST" autocomplete="off">          
<div class="text-field">
   Nombre de la asociacion
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxx password=xxx";
     $conn=pg_Connect($strconn);

  $consulta="Select n_asociacion from asociacion";
      $result=pg_query($conn,$consulta);
      while($results [] =pg_fetch_object($result));
      array_pop($results);?>

      <select name="asociacion_seleccion" id="i_clave_asociacion">
           <?php foreach ( $results as $option ) : ?>
           <option value="<?php echo $option->i_clave_asociacion; ?>"><?php echo $option->n_asociacion; ?></option>
            <?php endforeach; ?>
      </select>          
  </div>
</form>

This is just the dynamically selection box. Then I want to store the selected value in this variable:

$ingresaAsociacion       = pg_escape_string($_POST['asociacion_seleccion']);

So I can query the following statement:

$conocerIDasociacion     =  "SELECT N_ASOCIACION FROM ASOCIACION WHERE I_CLAVE_ASOCIACION='$ingresaAsociacion'";

I didn't want to use jQuery because the whole system is almost entirely made in PHP and HTML. Please, any help is welcome and I'm all ears to everyone.

Cheers!

Looks like you're missing a Submit button.

You do not have to use AJAX at all, nor jQuery. You can submit your form and process the selection value as you see fit.

The selected value in the select Tag will be sent to dar_alta_soniador.php, and that file will process that data, using the exact code you wrote:

$_POST['asociacion_seleccion']

So, in dar_alta_soniador.php you will write that code:

$ingresaAsociacion       = pg_escape_string($_POST['asociacion_seleccion']);

And then perform the query. You do not even have to worry about sending the data around in a session variable, POST does it for you already.

So everything should be OK in your code, or I may have misunderstood your question. Do you have an error message or get some inappropriate behavior?

Maybe the submit button is missing? I use a code like this:

For the select tag:

<div class="controls">
                                    <select name="list_name">
                                        <option>List</option>
                                        <?php 
                                            foreach ($nucleos as $inner_array) {
                                                $out = "<option>" . $inner_array['name'] . "</option>";

                                                echo $out;
                                            }
                                        ?>

                                    </select>
                                </div>

And for the Submit button:

<div class="control-group">
                                <div class="controls">                                 
                                    <button type="submit" class="btn">Confirm</button>
                                    <br/>                                 
                                </div>
                            </div>

</form>

I am using bootstrap here for style, HTML and CSS. Nothing more.

Best wishes,

I found another solution:

<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxxx password=xxxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);?>                     
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php 
while($row=pg_fetch_assoc($result)) 
{echo "<option>{$row['n_asociacion']}</option>";}?>
</select>       

The point is, whenever the user selects an option from the dyamically selection box, the value of the selection box is known if we call the value with pg_escape_string($_POST['NAME OF YOUR SELECTION BOX']);.

Thanks to all for your collaboration,my problem was resolved.