Error: "Please use POST request" with form and Javascript

I made a small form and I want it to send information back to the user when the inputs are left blank.

For some reason, he says use post request, so I added it, and now it gives me another error message.

Here is the code: http://jsfiddle.net/pwtnY/

<div id="main"> <form name="myForm" method="post"> <label><span>First Name:</span><input type="text" class="firstname" name="fname"></label><br/> <label><span>Last Name:</span><input type="text" class="lastname"></label><br> <label><span>E-Mail:</span><input type="text" class="email"></label><br/> <label><span>Phone:</span><input type="text" class="phone"></label><br/> <input type="submit" name="submit"> </form> <div id="answer"></div> </div> 

JavaScript:

 $(document).ready(function(){ $('input:submit').click(function(){ $firstname = $('.firstname').val(); $lastname = $('.lastname').val(); $email = $('.email').val(); $phone = $('.phone').val(); if($firstname === "" && $lastname==="" && $email ==="" && $phone === ""){ $("#answer").html("Please fill out all fields."); } }); }); 

Does anyone know what could be the problem and how can I solve it?

+4
source share
2 answers

When you click the submit button, the form is actually submitted to your django server. If you want to check the interface before sending it to the server, prohibit sending the submit button.

 $('input:submit').click(function(event){ event.preventDefault(); $firstname = $('.firstname').val(); 
+3
source

In your fiddle, you did not include the jquery library. If you do not want to send anything to the server, replace the submit button with a simple <input type="button" value="submit"> . Updated your example: fiddle

PS You probably want to use OR instead of AND in your check to check if any of the fields is empty.

0
source

All Articles