0

I'm completly new to js and I checked out several sites in here but couldn't find a matching answer 4 me. Problem: got array from django. I loop through and every entry will get his own submit button to "push" it in another js form. If I now hit the submit button just the 1 entry of the array will be submitted. As well this configuration shown below will reload the site always after pushing the button and discard the submitted result

<script type='text/javascript'>
function insert_entity() {
    insert_entity(kursLabel,LVbeginsAt,LVendsAt,LVlocation,Typ) {
    var record_already_exist=false;
    var d = new Date();
    var obj = new Object ();

    obj.kursLabel = document.getElementById("kursLabel").value.toLowerCase()    ;
    console.log(obj.kursLabel);
    ...
</script>




<body>
<main>
<mod>
{% if output %}
    {% for out in output %}
    <ul>
        <li>
            <form id=submit_form onsubmit="insert_entity({{out.kursLabel}});" >
            <input type="hidden" id="kursLabel" value="{{out.kursLabel}}">
            <input id="LVbeginsAt" value="{{out.LVbeginsAt}}">
            <input type="hidden" id="LVendsAt" value="{{out.LVendsAt}}">  
            <input type="hidden" id="LVlocation" value="{{out.LVlocation}}">
            <input type="hidden" id="Typ" value="{{out.Typ}}"> 
            <input type="hidden" id="LVdayOfWeek" value="{{out.LVdayOfWeek}}"> 
        <!--<button type="button" id="include_into_db" > {{out.kursLabel}} </button>-->
        <!--<input type="button" onclick="insert_entity(this.form)" value="{{out.kursLabel}}">-->
        <input type="submit" value="{{out.kursLabel}}">
        </form> 
        </li>
    </ul>
    {% endfor %}

</mod>
</main>
</body>

EDIT

my recent approach:

$(document).ready(function() {
    $( "#submit_form" ).submit(function() {
        $.ajax({
            type: "POST",
            //url: "/hello/",  // or just url: "/my-url/path/"
            data: {
                query: $( "#kursLabel" ).val()   
            },
            success: function(data) {
                console.log(datea);
            }
        });
    });
});

<mod>
    {% if output %}
        {% for out in output %}
        <ul>
            <li>
            <!--<form id=submit_form2 onsubmit="insert_entity({{out.kursLabel}});" > -->
            <form id='submit_form' method="post" action="db_record" >{% csrf_token %}
                <input type="hidden" id="kursLabel" value="{{out.kursLabel}}">
                <input id="LVbeginsAt" value="{{out.LVbeginsAt}}">
                <input type="hidden" id="LVendsAt" value="{{out.LVendsAt}}">  
                <input type="hidden" id="LVlocation" value="{{out.LVlocation}}">
                <input type="hidden" id="Typ" value="{{out.Typ}}"> 
                <input type="hidden" id="LVdayOfWeek" value="{{out.LVdayOfWeek}}"> 
            <!--<button type="button" id="include_into_db" > {{out.kursLabel}} </button>-->
            <!--<input type="button" onclick="insert_entity(this.form)" value="{{out.kursLabel}}">-->
                <input type="submit" value="{{out.kursLabel}}">
            </form> 
            </li>
        </ul>
        {% endfor %}

    {% else %}
        <p>No polls are available.</p>
    {% endif %}

</mod> 

But I still get the whole db as console.log :/

Ste fan
  • 1
  • 1
  • You are ending up creating multiple forms and hence, would end up submitting one of them as an anomaly created from that. [Read](http://stackoverflow.com/questions/8712398/multiple-forms-or-multiple-submits-in-a-page) – Nagaraj Tantri Jun 30 '16 at 07:13
  • I recognized this but I don't have a solution for my problem. I tried also a submit-post command, hand over the desired variables as arguments but then my buttons at the Homepage will disappear – Ste fan Jul 11 '16 at 05:51

0 Answers0