1

Okay so I have two input tags with types as buttons.

<div class="buttons-for-trade-or-sell" style="display: flex; position:relative;left: 33px; top:14px">
<div class="button-to-trade">
    <input type="button" class="trade-btn" id="button-to-select-trade" name="trade1" value="Trade"></input>
</div>
<div class="button-to-sell">
    <input type="button" class="sell-btn" id="button-to-select-sell" name= "trade2" value="Sell"></input>
</div>

My problem is how do I send to the server side code which button is selected. For example, if the trade-btn is selected i want the boolean to be set to true in my view. Then from there I would handle it accordingly. Basically, how do I send which button is selected to the server-side? Thanks.

1 Answer 1

2

There are multiple ways to solve this. One of them is to use pure javascript and embed a hidden form_type in the form which specifies whether the form is trade or sell and assign each button to the function with different input parameter.

<from id="my_form" action="#" method="POST">
  {% csrf_token %}
  <input id='form_type' name='form_type' value='' class='d-none' style='display: None;'>
  <div class="buttons-for-trade-or-sell" style="display: flex; position:relative;left: 33px; top:14px">
  <div class="button-to-trade">
      <input type="button" onclick="update_value_and_submit('trade');" class="trade-btn" id="button-to-select-trade" name="trade1" value="Trade"></input>
  </div>
  <div class="button-to-sell">
      <input type="button" onclick="update_value_and_submit('sell');" class="sell-btn" id="button-to-select-sell" name= "trade2" value="Sell"></input>
  </div>
</form>

<script>
function update_value_and_submit(val){
  let ftype = document.getElementById('form_type');
  ftype.value = val;

  let my_form = document.getElementById('my_form');
  my_form.submit();
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.