Jump to content

Form submit issues


nuwandias

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...