First off, I'm very new to coding. I'm making a website on WordPress for myself and have run into the problem of needing something that's not available. So instead of paying someone for it, I've decided that I want to learn it!
I'm trying to make a progress bar with HTML, JS, and CSS that automatically updates after a user submits a number using a form. I don't know how to make the 'Submit' button work so that when the form is submitted, the number gets added to the progress bar's value.
For example: if the progress bar is at 50/100 and someone submits a 10 (for example, but it will be chosen by the user), how do I make it so the progress bar updates to 60/100? After this is submitted, I would want there to be a list of users under the updated progress bar who have submitted by first name and what number they submitted. I'm assuming I need some sort of function to connect JS to HTML and display code to display the list of names with numbers submitted.
This is what I have tried in JSFiddle so far. I'm stuck because whenever I hit submit it changes the window where it ran the code to just a white box:
HTML:
<h3>Quantity</h3>
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
</div>
<br>
<h5>Fill out the form below:</h5>
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname"><br>
Email:<br>
<input type="email" name="email"><br>
Quantity:<br>
<input type="number" name="quantity" min="1" max="10000"><br><br>
<form action="action_page.php">
Select images: <input type="file" name="img" multiple>
<br>
<input type="submit" value="Submit">
<input type="reset">
</form>
CSS:
.progress, .alert {
margin: 15px;
border-radius: 25px;}
JS:
var $progress = $('.progress');
var $progressBar = $('.progress-bar');
var $quantity = $('.quantity');
$progressBar.animate({width: "50"}, 100);
Thank you so much for your help and explanations in advanced!
Best Answer
So to accomplish what you want you should first of all use an Ajax request instead of submitting a form. This will keep the user on the same page, while allowing you to contact your server and exchanging data with it.
Basically what I did is as follows:
type="button"
so it won't submit the form and navigate away..50/200 * 100 == 25%
Note: For now I put it outside of the ajax request, because stackoverflow snippets don't allow ajax and also I don't have a server up. In your final result be sure to move the
animate
line to the ajax success, so that only if it succeeds you will add the progress.Hope this helps you achieve what you are looking for.