Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing my form inputs after submission

I've tried it a few different ways based on searches I've done on the subject and for some reason I can't get it to work. I just want my text inputs and textarea to clear after I hit the submit button.

Here's the code.

<div id="sidebar-info">     <form name="contact-form" action="formprocess.php" method="post" target="ninja-frame">         <h1>By Phone</h1>         <p id="by-phone">XXX-XXX-XXXX</p>         <h1>By Email</h1>         <p id="form-row">Name</p>         <input name="name" id="name" type="text" class="user-input" value="">         <p id="form-row">Email</p>         <input name="email" id="email" type="text" class="user-input" value="">         <p id="form-row">Message</p>         <textarea name="message" id="message" class="user-input" rows="10" maxlength="1500"></textarea>         <p>*Please fill out every field</p>         <input type="submit" value="Submit" id="submit" onclick="submitForm()">          <script>             function submitForm() {             document.contact-form.submit();             document.contact-form.reset();             }         </script>     </form> </div> 
like image 614
Szuturon Avatar asked Jan 29 '13 17:01

Szuturon


People also ask

How do you clear form values?

You can easily reset all form values using the HTML button using <input type=”reset”> attribute. Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes, etc.

How do you prevent a form from clearing fields on submit?

You can use preventDefault method of the event object.

How do you clear the input field after submitting Vue?

In vue. js, we use the v-model directive to create a two-way data binding between the input field and vue data property, so that we can clear an input field value by setting the empty string (" ") to the data property.


2 Answers

Your form is being submitted already as your button is type submit. Which in most browsers would result in a form submission and loading of the server response rather than executing javascript on the page.

Change the type of the submit button to a button. Also, as this button is given the id submit, it will cause a conflict with Javascript's submit function. Change the id of this button. Try something like

<input type="button" value="Submit" id="btnsubmit" onclick="submitForm()"> 

Another issue in this instance is that the name of the form contains a - dash. However, Javascript translates - as a minus.

You will need to either use array based notation or use document.getElementById() / document.getElementsByName(). The getElementById() function returns the element instance directly as Id is unique (but it requires an Id to be set). The getElementsByName() returns an array of values that have the same name. In this instance as we have not set an id, we can use the getElementsByName with index 0.

Try the following

function submitForm() {    // Get the first form with the name    // Usually the form name is not repeated    // but duplicate names are possible in HTML    // Therefore to work around the issue, enforce the correct index    var frm = document.getElementsByName('contact-form')[0];    frm.submit(); // Submit the form    frm.reset();  // Reset all form data    return false; // Prevent page refresh } 
like image 174
Kami Avatar answered Oct 02 '22 12:10

Kami


since you are using jquery library, i would advise you utilize the reset() method.

Firstly, add an id attribute to the form tag

<form id='myForm'> 

Then on completion, clear your input fields as:

$('#myForm')[0].reset(); 
like image 24
Rotimi Avatar answered Oct 02 '22 12:10

Rotimi