0

I got this code right here which executes the function validate() when submit is clicked. The function changes some of the text in the page. But I can't see the effect because the page automatically refreshes after submission:

<form name="myForm" onsubmit="validate(); return false;">
    Age: <input type="text" name="age" />
    Height (meters): <input type="text" name="height" />
    Weight (kilograms): <input type="text" name="weight" />
    <input type="submit" value="Submit">
</form>

How do I prevent the page from reloading after each submission?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Kevin Lloyd Bernal
  • 355
  • 3
  • 8
  • 24

3 Answers3

0

You can use a test button inside your form

<input type="button" value="Test button" onclick="validate();">

Once solved, remove the button.

You can also use Firebug (or equivalent) to add a breakpoint in your javascript code.

Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
0

The submit event fired by your form automatically initiates the form action. If no form action is declared, it refreshes the page. Your need to prevent this default action from occuring before validation, then submit the data after it has passed validation.

Add preventDefault() to your validation code.

Brian Vanderbusch
  • 3,313
  • 5
  • 31
  • 43
0

Make sure you add return false; in your validate() function.

That will prevent the form to be submitted.

Example :

 function validate() {
     //Validation code goes here
     return false;
 }
Rudy Lee
  • 438
  • 5
  • 18