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.