Validate a dynamic Combobox with PHP

3

Currently I have a dynamic combo where when selecting a State you throw the due Municipality to which that State belongs.

What I want is to validate from the server side that the selected municipality actually belongs to that state, since it is very easy to modify the combo by any other input.

Now if I know that the Select State of Mexico the Municipalities are: Texcoco, Chalco, La Paz to name a few, how would this validation:

Currently I have this like this:

if (!empty($_POST)) {
    $estado = $_POST['estado'];
    $municipio = $_POST['municipio'];

    if (!preg_match("/^México$|^Puebla$/", $estado)) {
        echo "<font color='red'>Estado Incorrecto";
    } else {
        echo "<font color='#2ecc71'>Estado Correcto";
    }

    $buscar = array ( "México" => "Chimalhuacán","Chicoloapan",
             "Puebla" => "Chila","Amozóc", );

    if($buscar[$estado] == $municipio){
        echo "<font color='#2ecc71'>Muy bien, el Municipio si pertenece al Estado Seleccionado";
    } else {
        echo "<font color='red'>Incorrecto, el Municipio no corresponde al Estado Seleccionado";
    }       
}

<form class = "form-horizontal" method = "POST" action ="<?php $_SERVER['PHP_SELF'] ?>" autocomplete ="off">

    <div class="form-group">
        <label for="text" class="col-sm-2 control-label">Estado</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="estado" name="estado" value="<?php if(isset($estado)) echo $estado ?>">
        </div>
    </div>
    <br>
    <div class="form-group">  
        <label for="text" class="col-sm-2 control-label">Municipio</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="municipio" name="municipio" value="<?php if(isset($municipio)) echo $municipio ?>">
        </div>
    </div>
    <button type="submit" name = "submit" class="btn"Actualizar</button>
</form>

But I still do not achieve my goal. I hope someone can help me solve this problem.

    
asked by Noctis 28.08.2018 в 00:11
source

2 answers

0

You have to do the arrays states. From this mode you can check if the municipality belongs to the state with in_array . Something like this:

$buscar = array ( "México" => array("Chimalhuacán","Chicoloapan"),
         "Puebla" => array("Chila","Amozóc") );

if(in_array($municipio, $buscar[$estado]))
{
   echo "<font color='#2ecc71'>Muy bien, el Municipio si pertenece al Estado Seleccionado";
} else {
   echo "<font color='red'>Incorrecto, el Municipio no corresponde al Estado Seleccionado";
}
    
answered by 28.08.2018 / 00:16
source
0

I would ask the array to search in a different way. I would put the municipality as a key, and the state as a value. This is:

$buscar = array(
    "Cimalhuacan" => "Mexico",
    "Chicoloapan" => "Mexico",
    "Chila" => "Puebla",
    "Amozoc" => "Puebla
);

This way, since you know the state and the municipality, you can ask for:

if ($buscar[$municipio] == $estado) {
   echo "<font color='#2ecc71'>Muy bien, el Municipio si pertenece al Estado Seleccionado";
} else {
    echo "<font color='red'>Incorrecto, el Municipio no corresponde al Estado Seleccionado";
}
    
answered by 28.08.2018 в 00:22