0

I am currently redirecting the user to payment confirmation page if cash is selected from the dropdown list.

<select class="custom-select " id="payment" name= "pay_method" onchange="SelFunction()" required>
   <option value=""> Select the Pay Method </option>
   <option value="credit card"> Credit Card </option>
   <option value="cash"> Cash </option>
</select>

<script>
  function SelFunction() {
    var x = document.getElementById("payment").value;
      if (x == 'cash') {
        window.location.href = "{% url 'payment_confirmation' %}";
      }
   };
 </script>

This code is not posting any values to views files. I have tried with both request.POST and request.GET methods and none are working. Is there a way I can fetch the values from a template?

Sainath
  • 58
  • 1
  • 8
  • Yes, but you are *explicitly* bypassing your form submission in your JS by setting the window location directly. Why are you doing that? – Daniel Roseman Feb 26 '19 at 14:51
  • @DanielRoseman Because, I don't want the users to redirect if they opt for other options. Am I doing anything wrong here? – Sainath Feb 26 '19 at 15:37

1 Answers1

0

As the other commenter mentioned, this should be handled inside a post which receives the form value and redirects accordingly.

However, if you insist on it behaving as you've described, one way to handle this would be to pass the value of the drop down to the django url as an argument. Then handle the argument in your view and redirect accordingly.

Django arguments in urls are explained here.

Since you are calling the url in javascript and may need to get the value into the url using javascript you may have to use a trick. See this thread for a solution for sneaking a javascript variable into a django url.

Rob
  • 1,656
  • 2
  • 17
  • 33
  • Thanks, Rob for the idea to pass the value in javascript. That trick will definitely work for my scenario. – Sainath Feb 26 '19 at 15:39
  • You're welcome. Feel free to mark this answer complete if it answers your question. :) – Rob Feb 26 '19 at 23:15