I have two inputs with options for each of them. The first one is drug/disease and the second list has a list of drugs and a list of diseases depending on the choice selected for the first list.
I also have nine "cards" with data that is called by post requests. When drugs is selected and submitted, the nine cards submit post requests to drug routes and the drug data appears. When diseases is selected and submitted, the nine cards have different post requests to disease routes. I got the drug cards working, but now I added diseases, and I can't make post requests to the disease cards.
I added an if
statement saying if the user selected Drug
, after a submit action, the user will make post requests to Drug
and after selecting Disease
, the user will make post requests to Disease
. HOwever all it does is refresh the page.
The expected results is cards loading with drug data when drug is selected and submitted, and cards loading with disease data when disease is selected and submitted. The Actual result is page refreshing.
<div class="form-group">
<select id="memoryType" class="form-control firstList">
<option value="select" selected="selected">Select</option>
<option value="drug">Drug</option>
<option value="disease">Disease</option>
</select>
</div>
<div class="form-group">
<select id="drugInput" class="form-control search-input secondList">
<option value="select" selected="selected">...</option>
</select>
<div class="form-group">
<button class="form-control" id="submit"><i class="fas fa-search"></i></button>
</div>
var selectedOption = $('#memoryType option:selected').val();
if (selectedOption.toLowerCase() == "drug") {
$("#submit").click(function(event) {
alert("Submitted")
var text = $("#drugInput option:selected").val();
var search = JSON.stringify({
"input": text
});
console.log("submit");
$.post("/L1000", search, function(data) {
$(".card-1").html(data);
})
$.post("/creeds_drugs", search, function(data) {
console.log(data);
$(".card-2").html(data);
})
$.post("/creeds_diseases", search, function(data) {
console.log(data);
$(".card-3").html(data);
})
$.post("/geneshot", search, function(data) {
console.log(data);
$(".card-4").html(data);
})
$.post("/gwas_drugs", search, function(data) {
console.log(data);
$(".card-5").html(data);
})
$.post("/x2kl1000", search, function(data) {
console.log(data);
$(".card-7").html(data);
})
$.post("/x2k_creeds", search, function(data) {
console.log(data);
$(".card-8").html(data);
})
$.post("/L1000_diseases", search, function(data) {
$(".card-1").html(data);
})
event.preventDefault();
})
} else if (selectedOption.toLowerCase() == 'disease') {
$("#submit").click(function(event) {
var text = $("#drugInput option:selected").val();
var search = JSON.stringify({
"input": text
});
console.log("submit");
$.post("/L1000_diseases", search, function(data) {
$(".card-1").html(data);
})
$.post("/creeds_dx_dx", search, function(data) {
console.log(data);
$(".card-3").html(data);
})
event.preventDefault();
})
}
The issue is because your logic is backwards with regard to the if
statement being outside the click
handler - as that means it will only ever run when the page loads. You instead need to make that if
condition check when the click
happens to know what the currently selected option is.
You're also repeating some common logic in both sides of the if
statement which can be DRY'd up. Try this:
$("#submit").click(function(e) {
e.preventDefault();
var selectedOption = $('#memoryType option:selected').val();
var text = $("#drugInput option:selected").val();
var search = JSON.stringify({ "input": text });
if (selectedOption.toLowerCase() == "drug") {
// $.post requests...
} else if (selectedOption.toLowerCase() == 'disease') {
// $.post requests...
}
})
However, while that fixes the logic, you should be aware that making that many AJAX requests to your server is bad practice. Given enough concurrent users you would effectively be DDOSing yourself. A much better idea, especially given that all the requests receive the same data, would be to make a single request and then return all drug/disease/geneshot etc information in that one request.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments