stockton Posted May 5, 2008 Share Posted May 5, 2008 I have a web page at http://www.watersprite.co.za/email.shtml which includes javascript to check that the user has filled all fields and only if all fields are completed call the mail.php script to send the message. It always calls the mail.php script even if the alert has appeared to show there is an error. Please tell me what my silly mistake is(it must be silly as I have this working in other places). <html> <head> <title> WaterSprite's E-Mail Page </title> <SCRIPT LANGUAGE="JavaScript"> function reset() { document.ws_email.name.value=""; document.ws_email.email.value=""; document.ws_email.subject.value=""; document.ws_email.content.value=""; } function submit_click() { var bError = false; var strErrorMsg = ""; if (ws_email.name.value == "") { strErrorMsg += "You must enter a name\n"; bError = true; } else if (ws_email.email.value == "") { strErrorMsg += "You must enter an email address\n"; bError = true; } else if (ws_email.subject.value == "") { strErrorMsg += "Please enter the subject\n"; bError = true; } else if (ws_email.content.value == "") { strErrorMsg += "We cannot help you unless you tell us what help you need\n"; bError = true; } if (bError == true) { alert(strErrorMsg); } else { ws_email.submit(); } } </script> </head> <BODY OnLoad="reset()"> <center><img src="assets/images/watersprites_logo.jpg" alt="WaterSprites email" /></center> <br /> <H1 ALIGN=center>Send E-mail to WaterSprite</H1> <pre> <form name="ws_email" ID="ws_email" action=mail.php method=post> Your Full Name : <input type="text" name="name"> Your EMail address : <input type="text" name="email"> Subject : <input type="text" name="subject"> Your EMail message : <textarea rows=6 cols=65 name="content" ></textarea> <center> <input type="submit" value="Submit" onclick="submit_click()"> <input type="reset" value="Clear"> </center> </form> </pre> <hr width=50%> <P ALIGN=RIGHT>Page last revised on <!--#echo var="LAST_MODIFIED"--> </body> </html> Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted May 5, 2008 Share Posted May 5, 2008 I'm having the same problem. I borrowed some of the function you have here to validate my form. My idea was to have an action listener that only show the submit button when all required fields are filled. That probably doesn't answer your question but I thought you might like to hear the idea. Quote Link to comment Share on other sites More sharing options...
xenophobia Posted May 6, 2008 Share Posted May 6, 2008 On your button onclick event, do this: <input type="submit" value="Submit" onclick="submit_click(); return false;"> Add a return false at the end of it. This will prevent the form to proceed. Quote Link to comment Share on other sites More sharing options...
stockton Posted May 6, 2008 Author Share Posted May 6, 2008 Thank you for your suggestion but I have done as you suggest by adding a return false but it still fails. That line now looks like:- <input type="submit" value="Submit" onclick="submit_click()"; return false;> Quote Link to comment Share on other sites More sharing options...
stockton Posted May 6, 2008 Author Share Posted May 6, 2008 I think that maybe I should read a bit more carefully and see where your quotes started and finished. <input type="submit" value="Submit" onclick="submit_click(); return false;"> rather than <input type="submit" value="Submit" onclick="submit_click()"; return false;> Thanks for the help xenophobia. Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted May 6, 2008 Share Posted May 6, 2008 Thanks xeno, you actually answered one of my questions from a different post. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.