简体   繁体   English

将表单内外的数据保存到数据库中

[英]Saving data inside and outside a form into a database

I'm learning to code with Django. 我正在学习使用Django进行编码。 So I have a form that displays quizzes and a JavaScript file that computes these quizzes. 因此,我有一个显示测验的表单和一个计算这些测验的JavaScript文件。 The quizzes are displayed in a <form> , and I have other details like course still displayed on the form. 测验显示在<form> ,我还有其他详细信息,例如课程仍显示在表单上。 To the best of my knowledge, POST only takes data within the form. 据我所知,POST仅采用表格中的数据。 What if I have the score outside the form, how do I go about it? 如果我在表格之外获得分数,该怎么办?

This is what I've done already: 这是我已经做的:

The JavaScript that computes the results: 计算结果的JavaScript:

function submitAnswers(answers) {

var total = answers.length;
var score = 0;
var choice = []

//new dynamic method 1
for (var i = 1; i <= total; i++) {

    choice[i] = document.forms["quizForm"]["q" + i].value;
}


for (i = 1; i <= total; i++) {
    if (choice[i] == null || choice[i] == '') {
        alert('You skipped question ' + i);
        return false;
    }
}


// new dynamic method for checking answer
for (i = 1; i <= total; i++) {
    if (choice[i] == answers[i - 1]) {
        score++;
    }
}

//Display Result
var results = document.getElementById('results');
results.innerHTML = "<h3>You scored <span>" + score + "</span> out of <span>" + total + "</span></h3>"
alert("You scored " + score + " out of " + total);

var scoreOnly = document.getElementById('scoreOnly');
scoreOnly.innerHTML = score;

document.getElementById('btnSubmit').style.display = 'none';

return false;
}

The HTML that carries the questions, answers, course, as Wells as the score: 带有问题,答案,课程以及分数的HTML:

//Gets its value after JavaScript has finished computing.
<div class="text-blue text-darken-3" id="results"></div>
//This div scores only the score. No additional information.
                    <div class="text-blue text-darken-3" id="scoreOnly" name="scoreOnly" hidden></div>
                    <hr>
                    <form method="POST" name="quizForm" action="/quizzer/"
                        onsubmit="return submitAnswers(answers = [{% for q in questn %}'{{ q.answer }}',{% endfor %}])">
                        {% csrf_token %}
                        <div name="canditate">{{user.email}}</div>
                        {% for q in questn %}
                       <div name="course">{{q.course}}</div>
                        <h5> {{ q.question|safe }} </h5>
                        <p><label><input class="with-gap" name="q{{ q.id }}" type="radio" value="a" /><span
                                    class="blue-text text-darken-2"> {{ q.option_a|safe }}</span></label></p>
                        <p><label><input class="with-gap" name="q{{ q.id }}" type="radio" value="b" /><span
                                    class="blue-text text-darken-2"> {{ q.option_b|safe }}</span></label></p>
                        <p><label><input class="with-gap" name="q{{ q.id }}" type="radio" value="c" /><span
                                    class="blue-text text-darken-2"> {{ q.option_c|safe }}</span></label></p>
                        <br>
                        <hr>
                        {% endfor %}
                        <br>
                        <center><button class="btn blue darken-3" type="submit" id="btnSubmit">SUBMIT</button></center>
                    </form>

The code I tried: 我试过的代码:

def index(request):
    if request.method == "GET":
        context = {}
        questn = Questions.objects.all()
        context['title'] = 'Exams'
        context['questn'] = questn
        return render(request, "quizzer/index.html", context)

    if request.method == "POST":
        # print(request.POST)
        course = request.POST["course"]
        scoreResult = request.POST["scoreOnly"]

        theScores = ExamScores(course=course, scoreResult=scoreResult)
        theScores.save()
        return render(request, "quizzer/index.html", context)

What I want is a way to post both items outside and inside the <form> into the database. 我想要的是一种将<form>内外的项目都发布到数据库中的方法。

You have a two kind of solution. 您有两种解决方案。

first: making post requst with ajax . 首先:使用ajax进行发布。

second: it is possible to add form attribute to inputs that placed outside of form. 第二:可以将表单属性添加到放置在表单外部的输入。 You can get it here . 你可以在这里得到。

But: I hope you need to calculate the scores of quizzes in server side. 但是:我希望您需要在服务器端计算测验分数。 Because in terms of security and correct logical approach. 因为在安全性和正确的逻辑上来讲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM