0

I would like to generate the URL based on the form values. The problem is that when I click the submit button, I only get the partial URL up until the ?, eg. base-url/comparison?. I want the URL to have the form value concatenated to the end of base URL. How can I achieve this?

$('#generateCorrespondence').on('submit', function(e) {
  e.preventDefault(); 
  
  let id = $('#input-type').val();
  let selection = $('#selection').val();
  let method = $('#experimental-method').val();
  let resolution = $('#structure-resolution').val();
  
  if (id == 'loop_id') {
    let formAction = $('#generateCorrespondence').attr('action');
    let url = formAction + "id=" + selection;
    $('#generateCorrespondence').attr('action', url);
    
    console.log(url);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="form-horizontal col-sm-12" id="generateCorrespondence" action="base-url/comparison?">

  <div class="form-group col-sm-5">
    <label for="ife">Choose Selection Type</label>
    <select class="form-control" id="input-type">
      <option value="loop_id">loop_id</option>
      <option value="unit_id">unit_id</option>
    </select>
    <br />

    <label for="selection">Enter Selection</label>
    <input type="text" class="form-control" id="selection">
    <br />

    <label for="structure-resolution">Choose Equivalence Class Resolution Threshold</label>
    <select class="form-control" id="structure-resolution">
      <option value="4.0" selected>4.0</option>
      <option value="1.5">1.5</option>
      <option value="2.0">2.0</option>
      <option value="2.5">2.5</option>
      <option value="3.0">3.0</option>
      <option value="3.5">3.5</option>
      <option value="all">all</option>
    </select>
    <br />


    <label for="experimental-method">Choose Experimental Technique</label>
    <select class="form-control" id="experimental-method">
      <option value="all" selected>Any</option>
      <option value="xray">X-Ray</option>
      <option value="em">Cryo-EM</option>
    </select>
    <br />

    <button type="submit" id="submit_test" class="btn btn-primary">Submit</button>
  </div>
</form>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
TheEmperor
  • 335
  • 2
  • 12
  • 1
    Do it the right way and give the elements a `name` and the browser will do this automatically for you. – Andreas Jul 14 '21 at 08:56
  • I created a snippet from the code in the question where it already appears to be working as you require. Can you please edit the question to include the code which creates the issue you describe. Also, why are you using JS for this? Add `name` attributes to the inputs and you get this behaviour by default. – Rory McCrossan Jul 14 '21 at 08:56
  • 1
    Does this question help? https://stackoverflow.com/q/28333613/14032355 – ikhvjs Jul 14 '21 at 09:13

0 Answers0