Click to See Complete Forum and Search --> : Help with Forms
dodge-i
05-31-2008, 01:53 PM
I have this form and on it is a check box. The check
box will need to be check before the user can continue
on to the next page.
The submit button has an onclick event handler that will
run a javascript that will check to see if the check box
is checked. If it is not checked, a msg saying please
check the check box will be displayed next to the check
box.
I need help in setting up how I can run the javascript and
then submit the page to the server. Only when the checkbox
is checked will the submit button send data to the server. If
the check box is not checked and the user clicks submit, then
the javascript will be executed and no data will be sent yet.
ryanbutler
05-31-2008, 05:16 PM
Pretty simple...
add this script in b/w the opening and closing <head> tags:
<script type="text/javascript">
function checkme(){
if(!document.myForm.mycheckboxname.checked){
alert("You need to select the checkbox before continuing");
return false;
}
else{
return true;
}
}
</script>
Inside the opening <form> tag:
<form method="post" onsubmit="return checkme(); return false;">
Then the submit button:
<input type="submit" value="submit">
dodge-i
06-02-2008, 12:03 PM
This is my script,
<script language="javascript">
function showerror(){
if(document.terms.accept.checked == false){
document.getElementById("error").style.visibility = "visible";
}
}
</script>
This is the body of my form,
<form name="terms" id="terms">
<table width="586" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" width="30" valign="top"><input name="accept" type="checkbox" /></td>
<td valign="middle"><p>I accept the Terms & Conditions.</p></td>
</tr>
</table>
<table width="586" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" width="125"><a onclick="showerror()" style="cursor:pointer;"><img src="images/access_btn.gif" width="116" height="23" alt="Access the Web" border="0"/></a></td>
<td align="left"><p class="error" id="error">Please accept the Terms & Conditions.</p></td>
</tr>
</table></form>
If I put an action command in the form tag the show error doesnt execute
prior to the redirect.
What do I need to do to get it to check for an error and then redirect to
the site in the action command?
ray326
06-02-2008, 11:02 PM
Put the action in the form tag along with an onsubmit handler to check the acceptance. If it fails return false from the handler, otherwise return true and the form will submit.
cmpcreations
06-03-2008, 12:22 AM
Hi there.
I would really separate your javascript behavior from your markup.
shoot me a message or google 'unobtrusive javascript'
I'm currently working on a project and had to do a lot of validation; checking if check boxes are checked etc before we allow them to 'save' or submit a form.