Take 2 select value and display the data of each

Asked

Viewed 498 times

1

I’m putting together a page where the person will be able to compare 2 products.

I have 2 tables, categories and products.

The 1st combobox, make a select in the table of categories. When choosing a category, it calls a file that sees which products belong to that category.

So when choosing 2 products in the 2nd combobox(sublist) and pressing the button would have to be performed a select in the database, taking the 2 id’s of each sublist and display their data below.

Basically it would work like this:

<combo01> ComboBox 01</combo>
<combo02> ComboBox 02 </combo>
<combo01> sublist 01</combo>
<combo02> sublist 02</combo> //o conteúdo das sublists altera de acordo com o que foi selecionado no comboBox
<btn>Comparar</btn> //ao clicar em comparar, faria um select no banco com os 2 id de cada sublist, e mostraria os dados abaixo. 

I’ll put the code link here, if anyone can help me solve this problem, I would be very grateful

https://www.dropbox.com/sh/7eupvel35rli2qg/AADJBHRFFprKS1lQNJ06ZcKca?dl=0

  • Dude, the ComboBox 01 and the ComboBox 02 are to choose categories? because if yes, you will be able to compare different product categories, the ideal would be you have only one category combobox, and after that take the product make two selects to use in comparison.

  • Good afternoon Erlon ,Sorry to reply now , I ran out of internet yesterday. The problem of making 1 combobox only , is that if one wants to do search of different brands/ categories , would not work .

  • It all depends on what he used for

1 answer

1


I would do with ajax, passing the Ids of the products selected in the lists;

function buscaProduto(callback, id) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "endereco/funcao", //substitua pela página em php que acessa o banco e retorna os dados
        data: "{'cdProduto':'" + id + "'}", //vamos supor que você tenha uma função na página acima que espere um parametro com o nome cdProduto
        dataType: "json",
        success: function (json) {
            callback(json);                
        },
        error: function (XMLHttpRequest, status, error) {
            console.log(XMLHttpRequest);
            console.log(status);
            console.log(error);
        }
    });    
}

Calling the above function for product 01:

buscaProduto( function (json) {
    carregaProduto($.parseJSON(json.d), divProduto01);
}, [id do produto selecionado na lista01]);

and for product 02:

buscaProduto( function (json) {
    carregaProduto($.parseJSON(json.d), divProduto02);
}, [id do produto selecionado na lista02]);

Displaying the returned database data:

function carregaProduto(dados, div) {
   if (dados != null) {
      aprensenta os dados na div informada!
   } else {
      aprensenta mensagem de produto não encontrado!
   }    
}

You can make a new function to run both searchProduct() in the onClick event of the button like this:

function btnClick() {    
    buscaProduto( function (json) {
        carregaProduto($.parseJSON(json.d), $("#divProduto01");
    }, $("#tv_assinatura").val());

    buscaProduto( function (json) {
        carregaProduto($.parseJSON(json.d), $("#divProduto02"));
    }, $("#tv_assinatura02").val());
}

Note: I am just showing you a way to do and not giving you a code ready, so you copy and paste in your project.

  • good afternoon Dil_oliveira , where I would put this ajax script on the page ? You could demonstrate in my code , I don’t know much of ajax . Thank you from Now

  • Dude, files with sensitive data like username and password should not be made publicly available, someone with malicious intent can do some damage. I suggest you change your password immediately.

  • Vish , vlw guy , I swear I had not noticed that the config to access the server were in the file , thank you very much. I tried to call its function , but it’s not working , from what I saw on the net, it would call it on the button, like : <input value="Compare" type="button" onClick="loadProduct();" /> ? . Once again, thank you

  • Good afternoon Dill , First of all, thank you very much for your attention in helping to solve my problem. I made the php script that took and removed the data,You can test it here http://www.script-beta.esy.es/functions/call_pacotes.php. The problem is that , the button that should compare the data,is not executing any action.Error : Uncaught Referenceerror: loadProduct is not defined(index):100 onclick. I’m going to put all the code here, Would you help me see where I’m going wrong? Thank you once again : https://www.dropbox.com/sh/7eupvel35rli2qg/ADJBHRFFprKS1lQNJ06ZcKca?dl=0

Browser other questions tagged

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