To assign the value of a variable that is in a Javascript function, for an SQL query.

Asked

Viewed 1,757 times

3

I have a javascript function that gets the value of the selected item in a combobox, and would like to pass this value as parameter to an SQL query to bring the data that is related to the value obtained in the combobox Selected on my Asp page, follows my javascript function and my SQL query.

function obterOpcaoSelecionada(){
//obter o elemento select
var formu = frmDados.cb_catinsumo.value;

//obter o índice da opção selecionada
var indiceSelecionada = elem.options.selectedIndex;
}

SQL code:

    SELECT 
    id_insumo,nome_comercial 
FROM 
    tb_insumo
WHERE 
    id_catinsumo = 
            (   SELECT 
                    id_catinsumo as id
                FROM
                    tb_catinsumo
                WHERE
                    categoria_insumo ='parametroJavaScritp' 
            ) 
ORDER BY 
    nome_comercial 
  • Everton, you’ll have to use AJAX. Do you know how to use it? Either pure javascript or use a library like jQuery or Mootools?

  • @Sergio never used AJAX, it would be good to learn ,I’m using pure javascript, but I can use a jQuery library if applicable.

  • You are using pure Asp or Asp.net?

  • I am using pure Asp @Marciano.Andrade.

  • What programming language are you using on Asp? You can use a 'hiddenfield' object and via javascript you fill this 'hiddenfield', reading it in codebehind. Example: I fill the 'hiddenfield' via javascript and in my C# code I read this 'hiddenfield' to search the database.

  • @Gustavuu was exactly what I was thinking, he does not need to create a [Webmethod] for this, following the way you said I believe to be the fastest

  • @Marciano.Andrade I am using Vbscript on Asp.

Show 2 more comments

1 answer

2

You can use jQuery.ajax() to perform this procedure.

Example

var request = $.ajax({
    url: "pagina_que_fara_a_consulta.php",
    type: "POST",
    data: { sua_variavel : conteudo },
    dataType: "html"
});

request.fail(function( jqXHR, textStatus ) {
    alert( "Request failed: " + textStatus );
});

See more in https://api.jquery.com/jQuery.ajax/

  • Read the tags, it is not using php, but rather Asp.

  • @Andrade this is an example, he is also not using "pagina_que_fara_a_query", it is up to him to make the changes according to their respective needs.

Browser other questions tagged

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