Jump to content

Form submit issues


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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...