1

In html,I have a form and I want to know the form value without submitting it or refreshing the page. Once user will click on it, I want to get the value of the form and then pass it to view.py. Its working while I'm using type='submit' but not working if I'm disabling the submit button by using type='button'. Please let me know how to get the value of the form without submitting it.

URL:

url(r'^segmentation',include('segmentation.urls'))

VIEWS:

def index(request):
.......
.....
if request.method == 'POST': # If the form has been submitted...
        html_node_number2=request.POST.get('url')
..........
......
return render(request, 'tree_home.html',context)

TREE_HOME.HTML: ..... ... FORM1:

<form method="post" enctype="multipart/form-data" > {% csrf_token %}
    {% for node in tuples %}
    <button class="open-button" onclick="openForm()" name="html_node_number" value=
    {{node.list1|safe}} type="button" id="test"> </button>

    <script type="text/javascript">
                                $(document).ready(function() {
                                    var url = 12; 'Used 12 instead of html_node_number to cross check but even 12 is not also passed to views.'
                                    $("#test").click(function() {
                                        $.ajax({
                                            url: "/segmentation",
                                            type: "POST",
                                            <!-- dataType: "json", -->
                                            data: {
                                                url: url,
                                                csrfmiddlewaretoken: '{{ csrf_token }}'
                                                },
                                            success : function(json) {
                                                alert("Successfully sent the URL to Django");
                                            },
                                            error : function(xhr,errmsg,err) {
                                                alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
                                            }
                                        });
                                    });
                                });
                            </script>
</form>

ERROR: Its generating html_node_number = NONE value

soumyastat
  • 21
  • 2
  • Just to add...Im not submitting the form1 but submitting only form2 but require info from both the form after submitting. – soumyastat Sep 08 '20 at 06:41
  • If I understand your problem correctly, shouldn't an AJAX request be able to do this? – Prakhar Varshney Sep 09 '20 at 05:28
  • Im not very familiar with AJAX coding. Would you please help me out a little on that? Also, is there anyway to get it by using javascript like below? Though the below code is not working. var html_node_number = $('#test').val(); – soumyastat Sep 09 '20 at 06:06
  • Try this https://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications if you have trouble understanding. And yes, you can use javascript to get the values inside input elements and make an ajax post request. Really depends on your use case. – Prakhar Varshney Sep 09 '20 at 07:37
  • Thanks Prakhar for your reply! Really Appreciate! I have added little bit more details about the code above. Below is the few points. You can see above that using "def index" I'm generating html index and where I have few forms. And once user click on any form...then I want that form number back (html_node_number) in the same "def index" views. I have used Ajax code in script within the form itself. But anyhow still I'm not getting the value. May be I'm missing any point over here which you have already said. Would you please look into this and help me out. – soumyastat Sep 19 '20 at 15:04

0 Answers0