Access values sent by HTML form in PHP

1

Sorry if I make an error, but I'm new to this forum.

I want to make a decision in a php block using a value of a select.

<?php
        $act = $_GET["act"];
        $save = $_GET["save"];
        if($act == 'agregar'){
            if($save == 'true'){


                //IF con las acciones del SELECT
                if("Variable"=='Centro de Cómputo'){
                    //Accion a realizar
                    $agregar=$s->agregar_solicitud();
                }
                if ("Varialble"=='Mantenimiento de Equipo') {
                    //Accion a realizar
                     $agregar=$s->agregar_solicitud_mantto();
                }
                echo "<script>window.location='/solicitud'; </script>";         
            }
    ?>

And I have the SELECT HTML

<div class="form-group">
                                        <label class="col-sm-2 control-label letras-login bajar-letra" style="text-align: left;">Solicitar a:</label>
                                        <div class="col-sm-10">
                                            <select name="bitacora" id="bitacora" onchange="habilitarCombo(this.value);" class="form-control" required>
                                                <option value="" style="display:none;">Elija una opcion</option>
                                                <option value="Centro de Cómputo">Centro de Cómputo</option>
                                                <option value="Mantenimiento de Equipo">Mantenimiento de Equipo</option>
                                            </select>   
                                        </div>
                                    </div>
    
asked by EVM 12.01.2017 в 07:40
source

1 answer

3

To start you have to know by what method you are going to send the value of this select to the PHP script to process it. I'm going to go to the simple, a POST form that takes care of it.

<form action="procesar.php?act=XXX&save=XXX" method="post">
  <div class="form-group">
    <label class="col-sm-2 control-label letras-login bajar-letra" style="text-align: left;">Solicitar a:</label>
    <div class="col-sm-10">
      <select name="bitacora" id="bitacora" onchange="habilitarCombo(this.value);" class="form-control" required>
        <option value="" style="display:none;">Elija una opcion</option>
        <option value="Centro de Cómputo">Centro de Cómputo</option>
        <option value="Mantenimiento de Equipo">Mantenimiento de Equipo</option>
      </select>
    </div>
  </div>
  <input type="submit" value="Enviar">
</form>

This will send by POST the value of all the elements contained in the form, which will cause that on the server side a special variable called $_POST is created, which is an array whose indexes correspond to each of these elements. To access these values you only have to access the index of this array corresponding to the element:

echo $_POST['bitacora'];

The value of this index will be determined by the value that you have given to the element in its attribute name . In this way, in your code you will have:

<?php
    $act = $_GET["act"];
    $save = $_GET["save"];
    if($act == 'agregar'){
        if($save == 'true'){


            //IF con las acciones del SELECT
            if($_POST['bitacora'] == 'Centro de Cómputo'){
                //Accion a realizar
                $agregar=$s->agregar_solicitud();
            }
            if ($_POST['bitacora'] == 'Mantenimiento de Equipo') {
                //Accion a realizar
                 $agregar=$s->agregar_solicitud_mantto();
            }
            echo "<script>window.location='/solicitud'; </script>";         
        }
?>

This is the simplest way to access values sent from a form, although it is not the safest nor the most recommended. There are risks of code injection, among others. You can take a look at SO in Spanish to solve this type of problems or perform a Google search, there is a lot of information about it.

    
answered by 12.01.2017 / 08:19
source