How to call different post requests when submitting different form inputs

Happy Epicurean

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();
  })
}
Rory McCrossan

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to call API when submitting the form?

How to distinguish different form inputs in React?

How to reuse vue method for different form inputs

Submitting a form and getting results Python Requests Post

No POST request when submitting form

React - How to handle form submit for inputs in different components?

How to add data to a form when submitting via a POST AJAX request?

Form with two inputs not submitting?

how to call different servlet on clicking different button of html form

Same post requests different responses?

How to use Two forms in Django Templates, And how to call different function when it submit the form

How to test a function with different inputs

How do I transfer post requests to a different port with nginx?

Validating different types of form inputs with criterias

Send same inputs to different actions with one form

Adding inputs from a different model in a New form

Restrict duplicate form inputs across different forms

Angular 6 reuse form with different [(ngModel)] for inputs

Get the data of a form containing inputs with different names

Pug Form POST Body Empty When Submitting

How to submit a form when a different button is clicked

How to associate two different inputs collected in different times, and recall back them when it is needed?

Submitting form after validating inputs

I want set values to my html inputs via event emitter and when submitting the form the values are not binding . How solve the problem?

How to route requests on different ports to different controllers?

How can I send different types of data in ajax post call

Android Volley Caching with different POST requests

Multer: Different Storage Engines for multiple POST requests

How to post a different value from that shown in a select form input