submit form from a select

1

I have <select> with several options, while selecting an option, I want the form to be submitted to the same page and I'll get the value per php on the same page. For this, I have the following:

<?php
    if(isset($_POST['select_carros']))
    {
         echo $_POST['select_carros'];
    }
?>

<form action="" method="post" name="menuForm">
     <select name="select_carros">
          <option value="1">Carro 1</option>
          <option value="2">Carro 2</option>
          <option value="3">Carro 3</option>
     </select>
</form>

Basically, what I want is that when choosing the option the value of this option is printed. For example, I choose the option "Car 2" and it will be printed "2"

    
asked by anonymous 14.04.2014 / 16:44

3 answers

1

Complete Example with Last Choice Selection:

<form action="" method="post" name="menuForm">
     <select name="select_carros" onchange="document.forms['menuForm'].submit();">
         <option value="0">Escolha a Opção</option>
          <option value="1" <?php echo isset($_POST['select_carros']) && $_POST['select_carros']==1?' selected="selected"':'';?>>Carro 1</option>
          <option value="2" <?php echo isset($_POST['select_carros']) && $_POST['select_carros']==2?' selected="selected"':'';?>>Carro 2</option>
          <option value="3" <?php echo isset($_POST['select_carros']) && $_POST['select_carros']==3?' selected="selected"':'';?>>Carro 3</option>
     </select>
</form>


<?php
    if(isset($_POST['select_carros']))
    {
         echo $_POST['select_carros'];
    }
?>
    
14.04.2014 / 16:53
1

Just use onChange:

<form action="" method="post" name="menuForm" onchange="this.form.submit();">
    
14.04.2014 / 16:51
0

The action of your form should be submitted to the same page, so you can get the value "2" in $ _POST ['select_carros'].

<form action="mesma_pagina.php" method="post" name="menuForm">
    
14.04.2014 / 16:50