Generate automation of a function

Asked

Viewed 43 times

1

I have 5 tables:

pastores,
redes,
regioes,
areas,
setores,
celulas

In that order,

All five have these 2 fields in common, one of identification and the other that is the name.

idPastores, idRedes, ....
nome

I have this jQuery script down to popular a select of 

redes por pastor
regioes por redes,
etc...

I would like to set up a function that would allow me to do just one script for all of them.

Jquery

// JavaScript Document

$(document).ready(function (e) {

    $("#idPastor").on("change", function () {

        $.ajax({
            url: "_scripts/_php/_validacoes/buscarDados.php",
            type: "POST",
            dataType: "json",
            data: {
                  idPastor: $("#idPastor").val()
            },
            beforeSend: function() {
                $("#imgCarregando").css('display','block');
            },
            success: function (result) {
                $("#imgCarregando").css('display','none');

                $("#idRede").empty();

                if (result[0] == ""){
                    $("#idRede").append("<option value=>Sem Redes</option>");
                } else {
                    result.forEach(function(option){
                        $("#idRede").append("<option value=" + option["idRede"] + ">" + option["nome"] + "</option>")
                    });
                }
            }

        });


    });

});

PHP:

require_once "../../../config.php";

$redes = $redesDao->pesquisaRedesParametro("idPastor", $_POST["idPastor"]);

$options = null;
$i = 0;

if ($redes == null) $options [0] = "";
else {
         foreach ($redes as $rede):

            $options[$i]["idRede"] = $rede->getIdRede();
            $options[$i]["nome"]   = $rede->getNome();
            $i++;

         endforeach;

}

echo json_encode($options);

Can someone help me set up this function that would avoid doing 5 times the same script?

  • thanks for the Lipespry edition -off-

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.