Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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.

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    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

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •