Skip to content
Advertisement

I want to print the data the user has submitted in html form

I want to print the user’s submitted data after he click on the “Submit Your Entries” button

and display a massage for him says: Thanks ” Name” for your comments.. Your e-mail: example@yahoo.com Things you liked on my site: site design and Contents. for example

here’s my code:

<html>
  <head>
    <title>Feedback form</title>
<form method = "post" action = "">
    <title>Feedback form </title>

  </head>
  <body>

  <h1>Feedack Form</h1>
<p>Please fill out this form to help me improve My site.</p>

<p><lable>Name:
  <input name="name" type="text" size="20"/> <br>

  E-mail Addres:
    <input name="email" type="email" size="20"/>
  </lable>

  <p>
    <strong>Things you like on my site:</strong> <br>

    <label>Site design
<input name = "thingsliked" type = "checkbox" value = "Design" /></label>
<br>
<label>Contents
<input name = "thingsliked" type = "checkbox" value = "Contents" /></label>
<br>
 <label>Ease of use
<input name = "thingsliked" type = "checkbox" value = "Ease" /></label>
<br>


<input type = "submit"  value = "Submit Your Entries"/>
 <input type = "reset" value = "Clear Your Entries" />
  </body>
</html>

Advertisement

Answer

Have a look at this

  1. You need to wrap in a form before you can do submit and/or reset – your form tag cannot be in the head of the page
  2. I preventDefault here to not submit you will have to think of a way to do submit or the user cannot see the result – for examplel another submit button or using AJAX

document.getElementById("feedback").addEventListener("submit", function(e) {
  e.preventDefault(); // stop submission
  const html = [...this.elements]
    .filter(ele => {
      if (ele.type === "text" || ele.type==="email") return true;
      if (ele.type === "checkbox" && ele.checked) return true;
      if (ele.type === "submit" || ele.type === "reset") return false;
    })
    .map(ele => {
      const name = ele.name
      const value = ele.value;
      if (ele.classList.contains("thingslike")) return `<tr class="thingslike"><td>${ele.name}</td><td>&nbsp;</td></tr>`
      else return `<tr><td>${ele.name}</td><td>${ele.value}</td></tr>`
    }).join("");
  document.getElementById("res").innerHTML = `You answered: <br/><table id="answer">
   <tbody>${html}</tbody>
   </table>`;
   const like = document.querySelector("#answer .thingslike");
   if (like) like.insertAdjacentHTML("beforebegin",`<tr><td colspan="2">Things you like:</td></tr>`)
})
<h1>Feedback Form</h1>
<p>Please fill out this form to help me improve My site.</p>
<form id="feedback">
  <p>
    <label>Name:
    <input name="name" type="text" size="20" /> <br> E-mail Addres:
    <input name="email" type="email" size="20" />
  </label>

    <p>
      <strong>Things you like on my site:</strong> <br>

      <label>Site design
<input name="Design" class="thingslike" type="checkbox" /></label>
      <br>
      <label>Contents
<input name="Contents" class="thingslike" type="checkbox" /></label>
      <br>
      <label>Ease of use
<input name="Ease of use" class="thingslike" type="checkbox" /></label>
      <br>
      <input type="submit" value="Submit Your Entries" />
      <input type="reset" value="Clear Your Entries" />
</form>

<div id="res"></div>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement