I have been stuck on this issue for a while now and none of the solutions I try really work. This is what I need: The user fills out a form with textboxes, checkboxes and a dropdown-menu. Once the user clicks "submit", a summary of all the input should be shown (either on a new page or at least in a new div so I can hide it until the form has been submitted).
This is the form (I left out some of the input fields):
<form id="myForm">
<div class="part1">
<p><label for="vorname">Vorname:</label>
<input type="text" id="vorname" />
</p>
<label for="nachname">Nachname:</label>
<input type="text" id="nachname" />
<p>
<label for="strasse">Strasse und Hausnummer:</label>
<input type="text" id="strasse" />
</p>
<p>
<ul class="artikel">
<li><input type="checkbox" id="checkbox1" value="jacken" /><label
for="jacken">Jacken</label</li>
<li>
<input type="checkbox" id="checkbox2" value="hemden" /><label
for="hemden">Hemden</label ></li>
<label for="gebiet">Wählen Sie ein Krisengebiet aus:</label>
<select id="optionen">
<option value="option1">Syrien</option>
<option value="option2">Ukraine</option><br>
<input type="submit" value="Absenden" onclick="showSummary(); return false">
</div>
</form>
And this is the JS:
function showSummary() {
var vorname = document.getElementById("vorname").value;
var nachname = document.getElementById("nachname").value;
var strasse = document.getElementById("strasse").value;
var zusammenfassung = "Vorname: " + vorname + "<br>" +
"Nachname: " + nachname + "<br>" +
"Straße: " + strasse + "<br>"
var checkbox1 = document.getElementById("checkbox1").checked;
var checkbox2 = document.getElementById("checkbox2").checked;
var optionen = document.getElementById("optionen").value;
document.getElementById("zusammenfassung").innerHTML = zusammenfassung;
}
4
Answers
I suppose the problem is that the page is refreshed when you click the button. In this case, you can try stopPropagation or preventDefault methods of event.
Use it like this:
When you submit the form the page will be reloaded.
You should prevent this default behaviour, like so:
You missed the div which have id= zusammenfassung, So add this tag
<div id="zusammenfassung" >< /div> after form.
and I also added code for other options. Please Run Snippets below
You could also look at "POST" to a PHP file that outputs the input variables as variables?
then a file called form-result.php
You can obviously adapt this to your own code but gives you a very basic understanding of both methods. I personally LOVE the PHP method but each to their own.