I want to get the minutes and seconds from the timer in my template to my view.
I already tried different approaches with an ajax.post request but it didnt really work how I wanted it to be.
Here is my template:
{% block content %} <!-- Timer function --> <script type="text/javascript"> var sec = 0; function pad ( val ) { return val > 9 ? val : "0" + val; } setInterval( function(){ $("#seconds").html(pad(++sec%60)); $("#minutes").html(pad(parseInt(sec/60,10))); }, 1000); </script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="minutes"></span>:<span id="seconds"></span> <form action="results" id=results method="POST"> <!-- after clicking on this button the minutes and seconds should also be sent as a POST request --> <div class="command"> <button type="submit" name="ctest_submit">Submit solution</button> </div> </form> {% endblock %}
Now I want to get the minutes and seconds to my view as a POST request after clicking on the submit button. My idea was it to hide the time in a input like this:
<input type="hidden" name="seconds" value="what should be in here???">
but I dont know what should be in value?
Advertisement
Answer
You can fill the input values inside your interval.
Give your inputs ids (let’s say #seconds-input
& #minutes-input
), and use
$('#minutes-input').val(pad(parseInt(sec/60,10))
,
and $('#seconds-input').val(pad(++sec%60)
inside the interval.
Even better, save the seconds and minutes as vars and then assign them to both spans and inputs.