views:

158

answers:

2

Languages: PHP,JQuery,MySQL

The question is "simple", I have a triple dependency with php and jquerys, the code works great, for inserting new information, but now I would like to make an update, so I need to load the current information first and I don't figure it out how to, I add some parts of the code, if any more parts needed ask me before voting me down please.

Code is something like this:

      <tr>
        <td class="main"><?php echo ENTRY_COUNTRY; ?></td>
        <td class="main">
                    <select id="country" name="country">
                        <option value="0">Select One...</option>
                    </select>
            </td>
      </tr>
      <tr>
        <td class="main"><?php echo ENTRY_STATE; ?></td>
        <td class="main">
                <select id="state" name="state">
                    <option value="0">Select One...</option>
                </select>
        </td>
      </tr>
          <tr>
        <td class="main"><?php echo ENTRY_CITY; ?></td>
        <td class="main">
                <select id="city" name="city">
                    <option value="0">Select One...</option>
                </select>
        </td>
      </tr>

The three are loaded from a db in mysql, by actions like:

<script type="text/javascript">
$(document).ready(function(){
    cargar_paises();
    $("#country").change(function(){dependencia_estado();});
    $("#state").change(function(){dependencia_ciudad();});
    $("#city").change(function(){dependencia_zip();});
    $("#state").attr("disabled",true);
    $("#city").attr("disabled",true);
});

function cargar_paises()
{
    $.get("scripts/cargar-paises.php", function(resultado){
        if(resultado == false)
        {
            alert("Error");
        }
        else
        {
            $('#country').append(resultado);            
        }
    }); 
}
function dependencia_estado()
{
    var code = $("#country").val();
    $.get("scripts/dependencia-estado.php", { code: code },
        function(resultado)
        {
            if(resultado == false)
            {
                alert("No se encontraron provincias para ese pais");
                $("#state").attr("disabled",true);
                document.getElementById("state").options.length=1;
            }
            else
            {
                $("#state").attr("disabled",false);
                document.getElementById("state").options.length=1;
                $('#state').append(resultado);          
            }
        }

    );
}
function dependencia_ciudad()
{
    var code = $("#state").val();
    $.get("scripts/dependencia-ciudades.php?", { code: code }, function(resultado){
        if(resultado == false)
        {
            alert("Error");
        }
        else
        {
            $("#city").attr("disabled",false);
            document.getElementById("city").options.length=1;
            $('#city').append(resultado);           
        }
    });

..... Thanks for everything ;) sorry for the english.

A: 

I guess you want something like this:

On the main page:

<script>
document.ready = function() {
    fillBasicInformation();
}
</script>

Now you would have to define a fillBasicInformation function, as follows:

//this should only be called in edit mode
fillBasicInformation = function() {
    //First fill country select
    //select the specific country
    //Fill state select
    //select specific state
    //fill city select
    //select specific city

    //now attach events to onChange event
    //1
}

You have to decide now how you will get and fill your fields, adding options to the appropriate, probabl through ajax, or maybe directly on the page load and selecting the right one (using val(), for example).

Rodrigo Gama
but I have dependency between combos, they are not loaded until the other have the info, and it takes almost a second to load the dependency :S
Saikios
document.ready assures the function will be called only after the whole document is ready. Isn't this enough? If it isn't, you could place the funcions that fill all the options inside the 'fillBasicInformation' function, and call it using an asynchronous call...
Rodrigo Gama
I will try it,it doesn't mother if each reload a part of the webpage? because they are relationed?
Saikios
I edit the code, sepecifying what the fillBasicInformation will probably look like. Attaching the events only after filling the options will avoid the reloads. It's your decision, though, whether you will make four requests to the server (one to get the page, another to the list of countries, and so on) or you will load them all at once.
Rodrigo Gama
A: 

And how can i select the dependent combos when i m in a edit form? i can t do it

Fede