[Fixed]-How to send a variable from javascript to View in django

1👍

Try something like this: Remember that you can’t have DOM elements with same Ids, so when you generate the second form each fields should have their unique id.

HTML

<form id="form1">
  <input id="name1" type="text" value="hola1"/> 
</form>

<form id="form2">
  <input id="name2" type="text" value="hola2"/> 
</form>

<form id="form3">
  <input id="name3" type="text" value="hola3"/> 
</form>

<button id="mySubmitButton">submit</button>

JQUERY

$(function(){

  $('#mySubmitButton').on('click', function(){

    // for each form in your html you can process then and save the information in an object 
    var form_data = {}

    $("form").each(function() {
       var input_id = $(this).find('input').attr('id');
         var input_value = $(this).find('input').val();

       form_data[input_id] = input_value;
        });

        console.log(form_data);

    $.post( "your django view url", {data: form_data}, function( data ) {
      // maybe show a success or fail message accordantly
    });

  });


});

Leave a comment