views:

51

answers:

3

Hi

I have written following function which checks whether start_date field is not empty and displays proper message when submit button is clicked. But then it takes the control to the previous page. So user has to write again all other fields on that form. Is there any way to stay on that page even after prompting the error message, with all other fields value.

function checkform() 
{
if(document.frmMr.start_date.value == "") 
{
    alert("please enter start_date");
    return false;
}
else
{
    document.frmMr.submit();
}
}

<html>
<form name=frmMr action="page1.jsp" >
Enter Start date:
<input type="text" size="15" name="start_date" id="start_date">
<input type="submit" name="continue" value="submit" onClick="checkform();">
</form>
</html>

Thanks in advance

A: 

Don't know for sure, but it sounds like it is still submitting. I quick solution would be to change your (guessing at your code here):

<input type="submit" value="Submit" onclick="checkform()">

to a button:

<input type="button" value="Submit" onclick="checkform()">

That way your form still gets submitted (from the else part of your checkform()) and it shouldn't be reloading the page.

There are other, perhaps better, ways of handling it but this works in the mean time.

WSkid
Actually over here i have given a little bit code but in the original one it doesn't get submitted the control goes to the previous page. When all the fields are filled then only gets submitted.And by changing "submit" to "button" is not working.
reva
+3  A: 

While you have a return value in checkform, it isn't being used anywhere - try using onclick="return checkform()" instead.

You may want to considering replacing this method with onsubmit="return checkform()" in the form tag instead, though both will work for clicking the button.

(Also, the form will submit if you return true - so you can just return true in the else condition.)
Hello all thanks a lot!! Its working now..
reva
@reva Then you should accept one of the answers. I also agree with smerriman, you should hook to onsubmit, not onclick. Also, check again on the server that "start_date" is not empty - javascript validation is a convenience for the end user, and can easily be bypassed by anyone attempting to hack your site, so you can't depend on it.
Stephen P
A: 

Can Anyone tell me how I would use this code but store the value of the text field so It can be sent via php?

So, i mean it gets the value of the field then somehow it can be sent like mypage.php?page=funzone&value_of_field=VALUE_HERE

thanks!

Ben
That's not an answer. That's a question. There's an `Ask Question` button at right top :) You should delete this "answer". Learn here more how Stackoverflow works: http://stackoverflow.com/faq
BalusC