Jump to content

Form being sent when not validated, need to stop it


Flic

Recommended Posts

Hi,www.gecofabrications.co.uk/contact.html is as you can see a contact form. This is validated using javascript and that works ok, however even when the form validation fails the form still gets sent!Anyone know why?Any help much appreciated!Flic
Try doing the following: Get rid of the onSubmit, and do onClick on teh html submit button, and also do instead of this, do this.form.works for me on my form submits. And you dont really need the back slashes (/) at the end of the inputs.
<div id="content"><h1>Email Us</h1><form name="contactform" method=post action="send.php"><p><label for="name" id="namel">Your name: *</label><input class="text" type="text" name="name" /></p><p><label for="email" id="emaill">Your email: *</label><input class="text" type="text" name="email" /></p><p><label for="tel" id="tell">Your phone number:</label><input class="text" type="text" name="tel" /></p><p><label for="ref" id="refl">Your reference:</label><input class="text" type="text" name="ref" /></p><p><label for="enq" id="enql">Your enquiry: *</label><textarea name="enq"></textarea></p><p> </p><p><input type="submit" value="Send Email" onClick="return check(this.form);"/></p></form></div>

Link to comment
Share on other sites

Its still sending it :)
Ok, get rid of the that return inside the onClick event.and i also check your code on the site, on the method=post part, there should be " " around postmethod="post" Maybe those will help, and another thing, what does that function check do?
Link to comment
Share on other sites

Don't mean to sound harsh but wallyson do you know what your talking about ? :)

And you dont really need the back slashes (/) at the end of the inputs.
Do you know xhtml ?
Maybe those will help, and another thing, what does that function check do?
That says it all... check() is validating the form input among other things
Try doing the following: Get rid of the onSubmit, and do onClick on teh html submit button,
Ok, get rid of the that return inside the onClick event.
That's just bad advice, why do that :) Essentially the best way (IMO) to validate a form is using onsubmit.
<head><script>function check(elmt){var outcome=true;if (elmt.one.value=="")outcome=false; //if form empty do not submitreturn outcome;}</script></head><body><form action="process.php" method="post" onsubmit="return check(this)"><input type="text" id="one" /><input type="submit" /></form></body>

Link to comment
Share on other sites

Don't mean to sound harsh but wallyson do you know what your talking about ? :) Do you know xhtml ?That says it all... check() is validating the form input among other thingsThat's just bad advice, why do that :) Essentially the best way (IMO) to validate a form is using onsubmit.
<head><script>function check(elmt){var outcome=true;if (elmt.one.value=="")outcome=false; //if form empty do not submitreturn outcome;}</script></head><body><form action="process.php" method="post" onsubmit="return check(this)"><input type="text" id="one" /><input type="submit" /></form></body>

you dont need / for this to execute, the onClick event works just fine, and what does check() do implied that he shows me the JS hes using. I use alot of DOM for my complex websites, so im used to using this.form and such. As with the return, you are correct in YOUR example, but it doesnt always have to be there.People write different code. who cares about xhtml, I know citigroup, and amazon dont.
Link to comment
Share on other sites

you dont need / for this to execute, the onClick event works just fine,
The problem wasn't with the execution, the problem was that the form still submitted if validation failed.
This is validated using javascript and that works ok, however even when the form validation fails the form still gets sent!
see :)
and what does check() do implied that he shows me the JS hes using.
You can already see it for yourself, javascript is client side so it's not hard to find:http://www.gecofabrications.co.uk/form_check.js
People write different code. who cares about xhtml, I know citigroup, and amazon dont.
Who? W3C andl most of us i'd imagine
Link to comment
Share on other sites

What is "I know citigroup, and amazon dont" supposed to mean?
What im saying is is that most business corporations who sell a product stick to traditional old standards so that people who have different types of browsers or older versions of browsers can still view the site. html 4.01 is being widely used in most corporations for this very reason.and most of the internet is business of some sort, so back to my statement: who cares if its not written in html vs xhtml.
Link to comment
Share on other sites

Eep, arguing on my thread!And its still not stopping it :) *Goes off to look at PHP validation*
flic, the code that scott100 provided will work just fine for checking the whole form. If you need more help check out the validation tutorial in the w3 area. Try to look at his code, and then mimic it with your style. If your doing something complicated his code may become of no use. Try google searching for a validation js file that can do it all: meaning if a particular field is not filled out, a red markup appears to show which one isnt. that sorta of thing.
flic, the code that scott100 provided will work just fine for checking the whole form. If you need more help check out the validation tutorial in the w3 area. Try to look at his code, and then mimic it with your style. If your doing something complicated his code may become of no use. Try google searching for a validation js file that can do it all: meaning if a particular field is not filled out, a red markup appears to show which one isnt. that sorta of thing.
I see you got it work, no?
Link to comment
Share on other sites

I see you got it work, no?
The validation always worked, it would come up with the errors but then continue to send it, which obviously I don't want. I've now got it backed up with PHP validating as I'm using PHP to send it anyway.However I would still like the javascript stopping it if the entries don't validate. I can't see anything which should mean its sending when it shouldn't be.
Link to comment
Share on other sites

Ok, just to let you know I've finally got it working perfectly!Apparently it worked as long as there was nothing in enquiry. If there was it would overwrite all the falses generated before.I've reworked the logic a bit and it did the trick!Yay :)

Link to comment
Share on other sites

ok to Flic: onsubmit=" return check()" Would be how you do it, i already know that you figured it out but still, its kinda sad when you have to go through all of this and end up doing fixing the problem on your own because the people who were helping you decided to start arguing.To the people who were arguing: PM-ing is meant for that. So are AIM, YAHOO, and MSN along with Gtalk. this person came here looking for help, not a debate. To make HTML valid by any means, yes you do need the (/) in the input tags, its just bad practice to not do. Also, onsubmit works if say someone is in the textbox writting something, and accidentally hits enter. Onclick only works when someone hits the button, and i dont know about you but i VERY rarely actually use the mouse to hit the submit button.Sheesh.

Link to comment
Share on other sites

ok to Flic: onsubmit=" return check()" Would be how you do it, i already know that you figured it out but still, its kinda sad when you have to go through all of this and end up doing fixing the problem on your own because the people who were helping you decided to start arguing.To the people who were arguing: PM-ing is meant for that. So are AIM, YAHOO, and MSN along with Gtalk. this person came here looking for help, not a debate. To make HTML valid by any means, yes you do need the (/) in the input tags, its just bad practice to not do. Also, onsubmit works if say someone is in the textbox writting something, and accidentally hits enter. Onclick only works when someone hits the button, and i dont know about you but i VERY rarely actually use the mouse to hit the submit button.Sheesh.
in all honesty I did not want to argue, I was just explaining what I meant to say, but quickly jumped off it. All I want to do was help, but unfortunatly some people dont understand that.Flic gratz on your code woring, I know you feel good.
Link to comment
Share on other sites

  • 2 months later...
To the people who were arguing: PM-ing is meant for that. So are AIM, YAHOO, and MSN along with Gtalk. this person came here looking for help, not a debate.
I'd prefer it if it was done in the open so it can be put an end to. Discussing is one thing, but arguing is just unnecessary and unhealthy for the community, so it's better to resolve issues in the open than to create hostility and enmity "behind the scenes" (over PM) which no one else knows the cause of. PM I think is more a function for off-topic chatting and, well, private matters. Of course arguing can be a private matter, but if it's over topics such as code solving and procedures, like I said, it's preferrable to do it in the topic that spurred the disagreement..
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...