View Full Version : Form checking ... again!!

12-07-2006, 09:45 PM

I have read many many posts on form checking with javascript, but I just can't seem to work out what I need. I don't know much about javascript at all. Most of my site is using php.

My problem:- I have a form that posts to another page when submitted. Now I need to add in a checkbox on the form. If the checkbox is ticked the form can be submitted to the next page, but if it is not ticked I want to have a warning popup with Yes No buttons on it. If they click Yes it will post to the next page, if they click No the form will not post, allowing them to either tick the box or make other changes.

Any ideas, hints, tips would be greatly appreciated.

12-08-2006, 01:21 AM
Hi Taipan

I wrote an example to help you out. Please copy and past the code in a document and save as a html file....so you can test it.

<title>Confirm Before Submit</title>
<script type="text/javascript">
function checkForm( frm ) {
// check if check_me checkbox is not selected.
if( !frm.check_me.checked ) {
var answer = window.confirm( "Are you sure? Please Confirm your action" );
if( answer ) {
return true;
} else {
return false;
} //-- ends else
} //-- ends if
return true;
} //-- ends function checkForm
<form method="get" action="" onSubmit="javascript: return checkForm(this);">
<input type="text" name="name" size="30"><br>
<input type="checkbox" name="check_me"/> Select Me or Else<br>
<input type="submit" name="submit" value="Submit Form">

good luck.

12-08-2006, 02:46 AM
Thanks so much Ess, I haven't tested it with my full form as yet to make sure all the information is posted to the next page (I don't see why it wouldn't), but changing the method="post" and action="next.php" it works perfectly.

I tried so many other examples before posting, it seems simple once I have been shown it. Much appreciated.

12-08-2006, 04:49 AM
I have started to put the code into my page and have just realised that I have more than one submit button. I only want it to check for the checkbox when one of the buttons is clicked, not the other. How can I work out which button was clicked?

EDIT:- Nevermind, using my usual coding method (trial and error) I finally worked out how to move the javascript call into the button itself.