www.webdeveloper.com
Results 1 to 2 of 2

Thread: First validate form and only after that load thank you page

  1. #1
    Join Date
    Apr 2011
    Posts
    8

    First validate form and only after that load thank you page

    Hello,

    I have a form and depending on what checkboxes are selected in the form, it loads a custom thank you page when the form is submited. No problem with this. The problem is that the form loads the thank you page BEFORE the form validating function is completed. How do I fix that? Example below:

    ***HTML form***

    <form action="my cgi script" name="example" method="post" onsubmit="return validate(this)">

    Boring form data, checkboxes and text fields.

    <input type="submit" onclick="redirect(this.form,'group1')" value="example" name="example" />

    *** Close HTML form***

    ***Javascript***

    function validate(form) {
    Many, boring, validation functions
    }

    function redirect (form, group) {
    var numberClicked = [];
    for (var i=0; i<form.elements.length; i++) {
    if (form.elements[i].name == group && form.elements[i].type == 'checkbox' && form.elements[i].checked)
    numberClicked.push(i);
    }
    var min=Math.min.apply(Math, numberClicked)+1;
    var max=Math.max.apply(Math, numberClicked)+1;
    location.href=min+"-"+max+".htm"
    }


    ***Close Javascript***


    Thanks!

  2. #2
    Join Date
    Mar 2011
    Posts
    1,133
    The 'onclick' function on the Submit button sends the user to the destination page immediately when the user clicks on it, so the <form> data never gets submitted. In theory, you could use AJAX to submit the <form> and then redirect the user, but that's bad practice because you'd always be saying "Thank-you" to the user regardless of the results of the script that processes the form data. Better to have your script redirect the user based on the results of processing the form.
    Rick Trethewey
    Rainbo Design

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles