nuwandias Posted September 17, 2007 Share Posted September 17, 2007 Hi Guys.. I've got a form, which submits to php file. The form tag and the submit button is shown below <form name="form1" id="form1" method="post" action="insert.php" onsubmit="return validate(this.form)"><input type="submit" value="Submit" name="subbut"/> I have written a testing function to see when this form gets submitted. This function is show below function validate(thisform){ [indent]if(thisform.name == ""){ alert("return true"); return true; } else{ alert("return false"); return false; }[/indent] } My issue is, whatever i return from the validate function, my form gets submitted. What i need to do is stop submitting the form when a certain condition occurs. I tried by using a button and calling the validate method on it's onclick event and i tried to submit the form by saying thisform.submit(); when my validations were over, but then it started giving an error message saying that the submit() method is not a property or function of the form object. This coding is show below function validate(thisform){ if(thisform.name == ""){ alert("true"); } else{ thisform.submit(); }}<input type="button" value="Submit" name="subbut" onclick="validate(this.form)"/> I've never encountered issues like this before. Specially the error which says that the submit function is not a property or method of the form object !!Is there anything wrong i'm doing here? If there is anyone who can help me out, please be kind enough to reply soon.Thanks in advance,Nuwan. Link to comment Share on other sites More sharing options...
george Posted September 17, 2007 Share Posted September 17, 2007 My guess is that you need to 'call' the php code from your javascript "validate" function using AJAX. Why I guess this? Because your form has two exits as it were, one, in the method= and one in the onsubmit=. I think both are acting on the fact that the form is being submitted. You get your call to your php method, and you get your javascript validation, but your call to your php code is not dependant on the validation. This is a guess. Link to comment Share on other sites More sharing options...
PrateekSaxena Posted September 17, 2007 Share Posted September 17, 2007 I went through a similar problem. See what I do is- <form onsubmit='checkForm(); return false;'><!--Whatever--></form> checkForm = function(){//do Validation//POST or GET through AJAX itself} Link to comment Share on other sites More sharing options...
justsomeguy Posted September 18, 2007 Share Posted September 18, 2007 Put the function on the onclick for the submit button instead of onsubmit for the form. If it returns false then the submit button shouldn't submit the form, or else it will. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.