How to find the value of a <select> pulled from the database

Asked

Viewed 147 times

0

I’m putting together a table of plans TABLE , where the "STICK TO THE PLAN" button changes link according to the options chosen from select us.

Two of the 4 select user should select, are STATE and CITY, these, not written in select option and yes are pulled from a database.

Therefore, in order for the join button to change links according to the selected options, you needed to know the value of each select option, which are pulled from the database.

Therefore, I would like to know if it is possible to discover the value of a select that is pulled from the database, or if there is some other way to mention the city, or state without being by value, which in that case does not exist.

Thank you.

   jQuery(function($){
        $('#volume, #tipo, #estados').change(function(){
        var volume = $('#volume').val();
        var tipo = $('#tipo').val();
        var estados = $(this).val();

            if (volume == "peq" && tipo == "visitas_quatro" && estados == "SP") {
          $('.Linkbotao').attr('href', 'site.com');
  • The Example I made below is exactly for this

  • Right, except in my case for every combination of city and state with the other options it would generate a link. For example: Estado (SP,) Cidade (São Paulo), Volume (50), Visitas (4) is a link. Already State (SC), City (Florianópolis), Volume (100), Visits (8) is another link. And so on. How will I set a link for each city with the code you posted below? At the moment I am doing so: if (volume == "peq" && type == "visitas_four" && states == "?" && cities == "?") { $('.Linkbotao'). attr('href','http://site.com'); where have "?" would be the value of every sick option and city

  • is doing the right way, is not working?

  • I haven’t tested it with the code you sent me. Since I don’t have much intimacy with the subject (js), I don’t know what information of the code you gave me to put there where the "?" , where would be the value. You know me to tell?

  • I’m gonna ride, baby

  • I edited the city code, take a look

  • Dude, I edited my question there and put my code so far. (Only the part that changes the button link) I’m racking my brain trying to understand this code you posted, but I can’t get it to fit my code. Can you not take this code that I posted as a base and give me an example on top of it of how it would be with the state SP selected and city São Paulo, for example. I saw that in your code there is something with the country, already in my table would be only the state and city even. Anyway I appreciate the attention you’ve given me. I’m almost there.

  • See if it gets better now, I updated.

  • Dude, it’s still a little complicated for me, sorry rsrs. I need to add that city, Function and state? To half lost there with the country in the middle, in my case would be only state and city, which are pulled from the bank, already the type and volume are listed in select, in the code itself. And where would I indicate the state and city for a link, for example that of são paulo - sp? If you can take a look at the table’s source code, you can see how I’m doing so far. The link is: https://piscinafacil.com.br/tabela_teste2.html

  • The country Voce can take off, ignore it, focus only on the last city, where I gave the example

  • It is the buttons JOIN that Voce wants to change?

  • That’s right, it’s the JOIN button link. It would change depending on the combination of options that is selected in the Selects. Each combination is a price(plan) and each price is a link to membership. You may notice that when selecting a given city the price varies, however when we develop the table, we forget that the button link would vary. I still haven’t been able to adapt this code to my code

  • https://jsbin.com/dokosiq/edit?html,js,output

  • I tried using this code there, replacing select with '#states' which is the select name of the state part. However, when I added the code the state select is no longer showing any state.

  • Probably because you didn’t implement it correctly

  • Did you use Let or var? if it was Let, use var (by guarantee), from any error? I saw your code from the link you gave me, and that’s exactly what I said

  • I tried both, Let and var. I edited my question there and put the code as it is at the moment. I joined the volume and type state part, because the link will change according to all options: volume, type, state, and city (which is not in the code yet). Now, as it is there, it is showing the list of states, however, when I select the options that are described there in the code (state:SP volume:50 and visits:4) the link is not changing. What I’m doing wrong. If you prefer you can look right at the page code: https://piscinafacil.com.br/tabela_teste2.html

Show 12 more comments

1 answer

0

It is possible yes. you can do so

$(function(){

    // Pais
    function pais(){
        $.ajax({
            type: 'GET',
            url: 'funcoes.php',
            data: {
                acao: 'pais'
            },
            dataType: 'json',
            success: function(data){
                console.log(data);

                for(i = 0; i < data.qtd; i++){

                    $('select[name=pais]').append('<option value="'+data.id[i]+'">'+data.pais[i]+'</option>');
                }
            }
        });
    }
    pais();

    // Estado
    function estado(pais){
        $.ajax({
            type: 'GET',
            url: 'funcoes.php',
            data: {
                acao: 'estado',
                id: pais
            },
            dataType: 'json',
            beforeSend: function(){
                $('select[name=estado]').html('<option>Carregando...</option>');
            },
            success: function(data){
                $('select[name=estado]').html('');
                $('select[name=estado]').append('<option>Selecione o estado</option>');
                for(i = 0; i < data.qtd; i++){
                    $('select[name=estado]').append('<option value="'+data.id[i]+'">'+data.estado[i]+'</option>');
                }
            }
        });
    }


    // Cidade
    function cidade(estado){
        $.ajax({
            type: 'GET',
            url: 'funcoes.php',
            data: {
                acao: 'cidade',
                id: estado
            },
            dataType: 'json',
            beforeSend: function(){
                $('select[name=cidade]').html('<option>Carregando...</option>');
            },
            success: function(data){
                $('select[name=cidade]').html('');
                $('select[name=cidade]').append('<option>Selecione a cidade</option>');
                for(i = 0; i < data.qtd; i++){
                    $('select[name=cidade]').append('<option value="'+data.id[i]+'">'+data.cidade[i]+'</option>');
                }
            }
        });
    }

    function verificar(volume, tipo, estados, cidades){
        if(volume == "peq" && tipo == "visitas_quatro" && estados == "?" && cidades == "?"){
            $('.Linkbotao').attr('href', 'google.com');
        }
    }


    $('select[name=pais]').change(function(){
        $('select[name=cidade]').val($("select[name=cidade] option:first-child").val());
        var id = $(this).val();
        estado(id);
    });

    $('select[name=estado]').change(function(){
        var idEstado = $(this).val();
        var cidade = $("select[name=cidade] option:first-child").val();
        var estado = $("select[name=estado] option:first-child").val();
        var tipo = $("select[name=cidade] option:first-child").val();
        var volume = $("select[name=estado] option:first-child").val();

        verificar(volume, tipo, estado, cidade)

        cidade(idEstado);
    });


});

Browser other questions tagged

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