Html Form Posting Values to Django Backend only when submitted twice, or rather hit back to form and then submit again

huangapple go评论74阅读模式
英文:

Html Form Posting Values to Django Backend only when submitted twice, or rather hit back to form and then submit again

问题

这是你提供的代码的翻译部分:

问题描述: 我遇到了一个情况,当我点击提交按钮后,表单不会在第一次点击时提交值,它确实跳转到下一个页面,但我的Django视图无法获取POST的值。但是,当我在浏览器上点击返回按钮然后再次点击提交按钮时,它可以工作;然后我可以在我的DJango视图中看到POST的值。

HTML表单部分:

<form action="" method="POST" id="quizform">
    {% csrf_token %}

    <fieldset class="cfield" id="box1">
        <p id="question" class="fs-title">1. {{data.0.question}}</p>
        <!-- ... 这里省略了一些HTML代码 ... -->
        <button type="button" title="回答此问题以启用按钮" name="next" class="mybtn" id="nextpage" disabled>下一页 <i class="fas fa-arrow-right"></i></button>
        <button type="submit" name="" class="mybtn" id="submitbtn">提交</button>
    </fieldset>
</form>

JavaScript部分:

<script>
    var x = {{datalength}};
    $('#submitbtn').hide()
    var y = 1;
    $('#nextpage').click(() => {
        $('#nextpage').prop('disabled', true);
        $('#nextpage').prop('title', "回答此问题以启用按钮");
        var next;
        if (x != y) {
            next = y + 1;
            $(`#box${y}`).hide()
            $(`#box${next}`).show()
            if (y < x) {
                ++y;
            }
        }
        if (y == x) {
            $('#nextpage').hide();
            $('#submitbtn').show();
        }
    })
    $('#previspage').click(() => {
        $('#submitbtn').hide();
        $('#nextpage').show();
        var pre = y - 1;
        if (pre <= 0) {
            return false;
        } else {
            $(`#box${pre}`).show()
            $(`#box${y}`).hide()
            if (y > pre) {
                --y;
            }
        }
    })
</script>

<script>
    // 答案提交处理程序
    $('.ans').change(function (e) {
        var $choice = $(this);
        var qid = $choice.attr('qid');
        var choice = $choice.val();
        var next_btn = document.getElementById('nextpage');
        $.ajax({
            type: 'GET',
            url: '/check-answer/' + qid + '/',
            success: function (data) {
                $('input[qid=' + qid + ']').each(function (index, obj) {
                    var image = document.getElementById("a" + (index + 1) + "-icon-" + qid);
                    $(obj).prop('disabled', true);
                    if ($(obj).val() === data.correct && $(obj).val()) {
                        if ($(obj).val() == choice) {
                            image.src = '/static/assets/img/correctcolored.png';
                        } else {
                            image.src = '/static/assets/img/correct.png';
                        }
                        image.hidden = false;
                    } else {
                        if ($(obj).val() == choice) {
                            image.src = '/static/assets/img/wrongcolored.png';
                        } else {
                            image.src = '/static/assets/img/wrong.png';
                        }
                        image.hidden = false;
                    }
                });
                $('#nextpage').prop('disabled', false);
                $('#nextpage').prop('title', "");
            },
            error: function (error) {
                console.log(error);
            },
        });
    });
    $('.multipleans').click(function (e) {
        var $submit = $(this);
        var qid = $submit.attr('qid');
        var $answer1 = document.getElementById("correct_answer1" + qid);
        var $answer2 = document.getElementById("correct_answer2" + qid);
        console.log($answer1.value);
        $.ajax({
            type: 'GET',
            url: '/check-answer/' + qid + '/',
            success: function (data) {
                console.log(data.correct2);
                var image1 = document.getElementById("c1-icon-" + qid);
                var image2 = document.getElementById("c2-icon-" + qid);
                if ($answer1.value === data.correct || $answer1.value === data.correct2) {
                    image1.src = '/static/assets/img/correctcolored.png';
                } else {
                    console.log("test");
                    image1.src = '/static/assets/img/wrongcolored.png';
                }
                image1.hidden = false;
                if (($answer2.value === data.correct || $answer2.value === data.correct2) && $answer1.value != $answer2.value) {
                    image2.src = '/static/assets/img/correctcolored.png';
                } else {
                    image2.src = '/static/assets/img/wrongcolored.png';
                }
                image2.hidden = false;
                $('#nextpage').prop('disabled', false);
                $('#nextpage').prop('title', "");
                $('input[qid=' + qid + ']').each(function (index, obj) {
                    var image = document.getElementById("a" + (index + 1) + "-icon-" + qid);
                    $(obj).prop('disabled', true);
                    if ($(obj).val() === data.correct || $(obj).val() === data.correct2) {
                        image.src = '/static/assets/img/correct.png';
                        image.hidden = false;
                    } else {
                        image.src = '/static/assets/img/wrong.png';
                        image.hidden = false;
                    }
                });
            },
            error: function (error) {
                console.log(error);
            },
        });
    });
</script>

你提供的代码涉及到HTML表单和JavaScript。如果你需要更多帮助或有其他问题,请告诉我。

英文:

So I have stumbled upon a case where my form does not post values after I click the submit button once, it goes to the next page indeed but my Django View can not fetch the post values.

But soon after I hit back button on browser and then clicks the submit button again it works ; I can then see the POSTed values in my DJango Views.

Here's my HTML form. (Please note I have removed the unnecessary code which is long)

  &lt;form action=&quot;&quot; method=&quot;POST&quot; id=&quot;quizform&quot;&gt;
{% csrf_token %}
&lt;fieldset class=&quot;cfield&quot; id=&quot;box1&quot;&gt;
&lt;p id=&quot;question&quot; class=&quot;fs-title&quot;&gt; 1. {{data.0.question}}&lt;/p&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;{% if data.0.correct_answer2 == &quot;&quot; %}col-12{% else %}col-9{% endif %}&quot;&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;label class=&quot;form-check-label&quot;&gt; &lt;img id=&quot;a1-icon-{{data.0.pk}}&quot; src=&quot;/static/assets/img/correct.png&quot; class=&quot;answer_icon&quot; width=&quot;20px&quot; style=&quot;z-index: 99;&quot; hidden&gt;
&lt;input id=&quot;a1{{data.pk}}&quot; style=&quot;z-index: -1;&quot; type=&quot;radio&quot; class=&quot;radiobox form-check-input radio-box ans&quot; name=&quot;q{{data.0.pk}}&quot; qid=&quot;{{ data.0.pk }}&quot; value=&quot;{{data.0.a1}}&quot; required {% if data.0.correct_answer2  != &quot;&quot; %}disabled{% endif %}&gt;
&lt;span class=&quot;cspan&quot; style=&quot;color: white;&quot;&gt;TES&lt;/span&gt;
&lt;/label&gt;
&lt;span id=&quot;a1&quot; class=&quot;cspan&quot;&gt;{% if data.0.a1_image.url != None %}&lt;img  src=&quot;{{ data.0.a1_image.url }}&quot; class=&quot;&quot; width=&quot;100px&quot;&gt;{% endif %} {{data.0.a1}} &lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;label class=&quot;form-check-label&quot;&gt;&lt;img id=&quot;a2-icon-{{data.0.pk}}&quot; src=&quot;/static/assets/img/correct.png&quot; class=&quot;answer_icon&quot; width=&quot;20px&quot; style=&quot;z-index: 99;&quot; hidden&gt;
&lt;input id=&quot;a2{{data.pk}}&quot; style=&quot;z-index: -1;&quot;  type=&quot;radio&quot; class=&quot;radiobox form-check-input radio-box ans&quot; name=&quot;q{{data.0.pk}}&quot; qid=&quot;{{ data.0.pk }}&quot; value=&quot;{{data.0.a2}}&quot; required {% if data.0.correct_answer2  != &quot;&quot; %}disabled{% endif %}&gt;
&lt;span class=&quot;cspan&quot;  style=&quot;color: white;&quot;&gt;TES&lt;/span&gt;
&lt;/label&gt;
&lt;span id=&quot;a1&quot; class=&quot;cspan&quot;&gt;{% if data.0.a2_image.url != None %}&lt;img  src=&quot;{{ data.0.a2_image.url }}&quot; class=&quot;&quot; width=&quot;100px&quot;&gt;{% endif %} {{data.0.a2}} &lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;label class=&quot;form-check-label&quot;&gt;&lt;img id=&quot;a3-icon-{{data.0.pk}}&quot; src=&quot;/static/assets/img/correct.png&quot; class=&quot;answer_icon&quot; width=&quot;20px&quot; style=&quot;z-index: 99;&quot; hidden&gt;
&lt;input id=&quot;a3{{data.pk}}&quot; style=&quot;z-index: -1;&quot;  type=&quot;radio&quot; class=&quot;radiobox form-check-input radio-box ans&quot; name=&quot;q{{data.0.pk}}&quot; qid=&quot;{{ data.0.pk }}&quot;  value=&quot;{{data.0.a3}}&quot; required {% if data.0.correct_answer2  != &quot;&quot; %}disabled{% endif %}&gt;
&lt;span class=&quot;cspan&quot;  style=&quot;color: white;&quot;&gt;TES&lt;/span&gt;
&lt;/label&gt;
&lt;span id=&quot;a1&quot; class=&quot;cspan&quot;&gt;{% if data.0.a3_image.url != None %}&lt;img src=&quot;{{ data.0.a3_image.url }}&quot; class=&quot;&quot; width=&quot;100px&quot;&gt;{% endif %} {{data.0.a3}} &lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;label class=&quot;form-check-label&quot;&gt;&lt;img id=&quot;a4-icon-{{data.0.pk}}&quot; src=&quot;/static/assets/img/correct.png&quot; class=&quot;answer_icon&quot; width=&quot;20px&quot; style=&quot;z-index: 99;&quot; hidden&gt;
&lt;input id=&quot;a4{{data.pk}}&quot; style=&quot;z-index: -1;&quot;  type=&quot;radio&quot; class=&quot;radiobox form-check-input radio-box ans&quot; name=&quot;q{{data.0.pk}}&quot; qid=&quot;{{ data.0.pk }}&quot; value=&quot;{{data.0.a4}}&quot; required {% if data.0.correct_answer2  != &quot;&quot; %}disabled{% endif %}&gt;
&lt;span class=&quot;cspan&quot;  style=&quot;color: white;&quot;&gt;TES&lt;/span&gt;
&lt;/label&gt;
&lt;span id=&quot;a1&quot; class=&quot;cspan&quot;&gt;{% if data.0.a4_image.url != None %}&lt;img src=&quot;{{ data.0.a4_image.url }}&quot; class=&quot;&quot; width=&quot;100px&quot;&gt;{% endif %} {{data.0.a4}} &lt;/span&gt;
&lt;/div&gt;
{% if data.0.a5 != &quot;&quot; %}
&lt;div class=&quot;form-check&quot;&gt;
&lt;label class=&quot;form-check-label&quot;&gt;&lt;img id=&quot;a5-icon-{{data.0.pk}}&quot; src=&quot;/static/assets/img/correct.png&quot; class=&quot;answer_icon&quot; width=&quot;20px&quot; style=&quot;z-index: 99;&quot; hidden&gt;
&lt;input id=&quot;a5{{data.pk}}&quot; style=&quot;z-index: -1;&quot;  type=&quot;radio&quot; class=&quot;radiobox form-check-input radio-box ans&quot; name=&quot;q{{data.0.pk}}&quot; qid=&quot;{{ data.0.pk }}&quot; value=&quot;{{data.0.a5}}&quot; required {% if data.0.correct_answer2  != &quot;&quot; %}disabled{% endif %}&gt;
&lt;span class=&quot;cspan&quot;  style=&quot;color: white;&quot;&gt;TES&lt;/span&gt;
&lt;/label&gt;
&lt;span id=&quot;a5&quot; class=&quot;cspan&quot;&gt;{% if data.0.a5_image.url != None %}&lt;img src=&quot;{{ data.0.a5_image.url }}&quot; class=&quot;&quot; width=&quot;100px&quot;&gt;{% endif %} {{data.0.a5}} &lt;/span&gt;
&lt;/div&gt;
{% endif %}
&lt;/div&gt;
{% if data.0.correct_answer2 != &quot;&quot; %}
&lt;div class=&quot;col-3&quot;&gt;
&lt;br&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;img id=&quot;c1-icon-{{data.0.pk}}&quot; src=&quot;/static/assets/img/correct.png&quot; class=&quot;select_icon&quot; width=&quot;20px&quot; style=&quot;z-index: 99;&quot; hidden&gt;
&lt;select name=&quot;correct_answer1{{data.0.pk}}&quot; id=&quot;correct_answer1{{data.0.pk}}&quot; class=&quot;form-control&quot;&gt;
&lt;option&gt;select&lt;/option&gt;
&lt;option value=&quot;{{data.0.a1}}&quot; data-ans-no=&quot;a1&quot;&gt;{{data.0.a1}}&lt;/option&gt;
&lt;option value=&quot;{{data.0.a2}}&quot; data-ans-no=&quot;a2&quot;&gt;{{data.0.a2}}&lt;/option&gt;
&lt;option value=&quot;{{data.0.a3}}&quot; data-ans-no=&quot;a3&quot;&gt;{{data.0.a3}}&lt;/option&gt;
&lt;option value=&quot;{{data.0.a4}}&quot; data-ans-no=&quot;a4&quot;&gt;{{data.0.a4}}&lt;/option&gt;
{% if data.0.a5 != &quot;&quot; %}
&lt;option value=&quot;{{data.0.a5}}&quot;  data-ans-no=&quot;a4&quot;&gt;{{data.0.a5}}&lt;/option&gt;
{% endif %}
&lt;/select&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;img id=&quot;c2-icon-{{data.0.pk}}&quot; src=&quot;/static/assets/img/correct.png&quot; class=&quot;select_icon&quot; width=&quot;20px&quot; style=&quot;z-index: 99;&quot; hidden&gt;
&lt;select name=&quot;correct_answer2{{data.0.pk}}&quot; id=&quot;correct_answer2{{data.0.pk}}&quot; data-ans-no=&quot;asd&quot; qid=&quot;{{ data.0.pk }}&quot; class=&quot;form-control&quot;&gt;
&lt;option&gt;select&lt;/option&gt;
&lt;option value=&quot;{{data.0.a1}}&quot; data-ans-no=&quot;a1&quot;&gt;{{data.0.a1}}&lt;/option&gt;
&lt;option value=&quot;{{data.0.a2}}&quot; data-ans-no=&quot;a2&quot;&gt;{{data.0.a2}}&lt;/option&gt;
&lt;option value=&quot;{{data.0.a3}}&quot; data-ans-no=&quot;a3&quot;&gt;{{data.0.a3}}&lt;/option&gt;
&lt;option value=&quot;{{data.0.a4}}&quot; data-ans-no=&quot;a4&quot;&gt;{{data.0.a4}}&lt;/option&gt;
{% if data.a5 != &quot;&quot; %}
&lt;option value=&quot;{{data.0.a5}}&quot;  data-ans-no=&quot;a4&quot;&gt;{{data.0.a5}}&lt;/option&gt;
{% endif %}
&lt;/select&gt;
&lt;/div&gt;
&lt;input class=&quot;btn btn-success multipleans&quot; qid=&quot;{{ data.0.pk }}&quot; type=&quot;button&quot; value=&quot;submit&quot;&gt;
&lt;/div&gt;
{% endif %}
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;button type=&quot;button&quot; title=&quot;answer this question to enable the button&quot; name=&quot;next&quot; class=&quot;mybtn&quot; id=&quot;nextpage&quot; disabled&gt; Next &lt;i class=&quot;fas fa-arrow-right&quot;&gt;&lt;/i&gt;&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;&quot; class=&quot;mybtn&quot; id=&quot;submitbtn&quot; &gt; Submit&lt;/button&gt;
&lt;/form&gt;

Below is the Javascript :

&lt;script&gt;
var x={{datalength}};
$(&#39;#submitbtn&#39;).hide()
var y=1;
$(&#39;#nextpage&#39;).click(()=&gt;{
$(&#39;#nextpage&#39;).prop(&#39;disabled&#39;,true);
$(&#39;#nextpage&#39;).prop(&#39;title&#39;,&quot;answer this question to enable the button&quot;);
var next;
if(x !=y){
next=y+1;
$(`#box${y}`).hide()
$(`#box${next}`).show()
if(y&lt;x){
++y;
}
}
if(y==x){
$(&#39;#nextpage&#39;).hide();
$(&#39;#submitbtn&#39;).show();
}
})
$(&#39;#previspage&#39;).click(()=&gt;{
$(&#39;#submitbtn&#39;).hide();
$(&#39;#nextpage&#39;).show();
var pre=y-1;
// $(&#39;#nextpage&#39;).hide();
// alert(x)
if(pre&lt;=0){
return false;
}
else{
$(`#box${pre}`).show()
$(`#box${y}`).hide()
if(y&gt;pre){
--y;
}
}
})
&lt;/script&gt;
&lt;script&gt;
// Answer submit handler
$(&#39;.ans&#39;).change(function (e) {
var $choice = $(this);
var qid = $choice.attr(&#39;qid&#39;);
var choice = $choice.val();
var next_btn = document.getElementById(&#39;nextpage&#39;);
$.ajax({
type: &#39;GET&#39;,
url: &#39;/check-answer/&#39; + qid + &#39;/&#39;,
success: function (data) {
$(&#39;input[qid=&#39; + qid + &#39;]&#39;).each(function (index, obj) {
var image = document.getElementById(&quot;a&quot;+(index+1)+&quot;-icon-&quot;+qid)
$(obj).prop(&#39;disabled&#39;, true);
if ($(obj).val() === data.correct &amp;&amp; $(obj).val()) {
if($(obj).val() == choice){
image.src = &#39;/static/assets/img/correctcolored.png&#39;;
}else{
image.src = &#39;/static/assets/img/correct.png&#39;;
}
image.hidden = false;
} else {
if($(obj).val() == choice){
image.src = &#39;/static/assets/img/wrongcolored.png&#39;;
}else{
image.src = &#39;/static/assets/img/wrong.png&#39;;
}
image.hidden = false;
}
});
$(&#39;#nextpage&#39;).prop(&#39;disabled&#39;,false);
$(&#39;#nextpage&#39;).prop(&#39;title&#39;,&quot;&quot;);
},
error: function (error) {
console.log(error);
},
});
});
$(&#39;.multipleans&#39;).click(function (e) {
var $submit = $(this);
var qid = $submit.attr(&#39;qid&#39;);
var $answer1 = document.getElementById(&quot;correct_answer1&quot;+qid);
var $answer2 = document.getElementById(&quot;correct_answer2&quot;+qid);
console.log($answer1.value)
$.ajax({
type: &#39;GET&#39;,
url: &#39;/check-answer/&#39; + qid + &#39;/&#39;,
success: function (data) {
console.log(data.correct2)
var image1 = document.getElementById(&quot;c1-icon-&quot;+qid)
var image2 = document.getElementById(&quot;c2-icon-&quot;+qid)
if ($answer1.value === data.correct || $answer1.value === data.correct2){
image1.src = &#39;/static/assets/img/correctcolored.png&#39;;
}else{
console.log(&quot;test&quot;)
image1.src = &#39;/static/assets/img/wrongcolored.png&#39;;
}
image1.hidden = false;
if (($answer2.value === data.correct || $answer2.value === data.correct2) &amp;&amp; $answer1.value != $answer2.value ){
image2.src = &#39;/static/assets/img/correctcolored.png&#39;;
}else{
image2.src = &#39;/static/assets/img/wrongcolored.png&#39;;
}
image2.hidden = false;
$(&#39;#nextpage&#39;).prop(&#39;disabled&#39;,false);
$(&#39;#nextpage&#39;).prop(&#39;title&#39;,&quot;&quot;);
$(&#39;input[qid=&#39; + qid + &#39;]&#39;).each(function (index, obj) {
var image = document.getElementById(&quot;a&quot;+(index+1)+&quot;-icon-&quot;+qid)
$(obj).prop(&#39;disabled&#39;, true);
if ($(obj).val() === data.correct || $(obj).val() === data.correct2) {
image.src = &#39;/static/assets/img/correct.png&#39;;
image.hidden = false;
} else {
image.src = &#39;/static/assets/img/wrong.png&#39;;
image.hidden = false;
}
});
},
error: function (error) {
console.log(error);
},
});
});
&lt;/script&gt;

I don't think the view has any problem as it is fetching the value correctly only after second form submit.

I had a look at this post , which is quite relevant problem, but I don't think I have any conflicting event handlers.

答案1

得分: 0

经过大量的调试后,我想到了一个有效的解决方案。

首先,我创建了表单的一个副本:

var userSubmissionForm = document.getElementById('quizform');

然后,我将表单输入元素添加到这个副本中:

const hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', "q" + qid);
hiddenField.setAttribute('value', choice);
userSubmissionForm.appendChild(hiddenField);

最后,我在单击提交按钮时提交了新表单:

userSubmissionForm.submit();

但我仍然不确定为什么第一次提交没有生效。

英文:

So after a lot of debugging I thought of a solution which worked.

Firstly I created a copy of the form.

var userSubmissionForm = document.getElementById(&#39;quizform&#39;);

Then I added the form input elements to this copy.

  const hiddenField = document.createElement(&#39;input&#39;);
hiddenField.setAttribute(&#39;type&#39;, &#39;hidden&#39;);
hiddenField.setAttribute(&#39;name&#39;, &quot;q&quot;+qid);
hiddenField.setAttribute(&#39;value&#39;, choice);
userSubmissionForm.appendChild(hiddenField);

Lastly I submitted the newform upon clicking the submit button.

  userSubmissionForm.submit();

But I am still unsure why the first submit did not work.

huangapple
  • 本文由 发表于 2023年3月4日 05:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定