Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2006, 08:45 PM   PM User | #1
Taipan
New Coder

 
Join Date: Mar 2004
Posts: 95
Thanks: 8
Thanked 0 Times in 0 Posts
Taipan is an unknown quantity at this point
Form checking ... again!!

Hi,

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.
Taipan is offline   Reply With Quote
Old 12-08-2006, 12:21 AM   PM User | #2
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
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.

Code:
<html>
<head>
<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
</script>
</head>
<body>
<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">
</form>
</body>
</html>
good luck.
Ess
ess is offline   Reply With Quote
Old 12-08-2006, 01:46 AM   PM User | #3
Taipan
New Coder

 
Join Date: Mar 2004
Posts: 95
Thanks: 8
Thanked 0 Times in 0 Posts
Taipan is an unknown quantity at this point
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.
Taipan is offline   Reply With Quote
Old 12-08-2006, 03:49 AM   PM User | #4
Taipan
New Coder

 
Join Date: Mar 2004
Posts: 95
Thanks: 8
Thanked 0 Times in 0 Posts
Taipan is an unknown quantity at this point
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.

Last edited by Taipan; 12-08-2006 at 07:59 PM..
Taipan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:19 PM.


Advertisement
Log in to turn off these ads.