SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help needed: multi page form validation

    Hi,

    I have a survey which is spread over 5 pages. Each page has a form with a next button (which actually submits the form) Each form recieves the values of the ones before from hidden form fields.

    I am having some trouble validating this. I have set the page up as follows:
    PHP Code:
    if(!$submit1 == "submit1" || $submit1 == "false"){
    include(
    "survey1.php");
    }

    if(
    $submit1 == "submit1"){
    include(
    "survey2.php");
    }

    if(
    $submit2 == "submit2"){
    include(
    "survey3.php");
    }

    if(
    $submit3 == "submit3"){
    include(
    "survey4.php");
    }

    if(
    $submit4 == "submit4"){
    include(
    "survey5.php");

    I need the validation so that if a field is missing PHO echo's out some text beside the form field. I have done this with the following code:

    PHP Code:
    if($submit2){
        if(!
    $Q1){
            
    $q1error "true";
            
    $error1 1;
        }
        if(
    $Q1){
            
    $error1 0;
        }

        if(!
    $Q2){
            
    $q2error "true";
            
    $error2 1;
        }
        if(
    $Q2){
            
    $error2 0;
        }

        if(!
    $Q3){
            
    $q3error "true";
            
    $error3 1;
        }
        if(
    $Q3){
            
    $error3 0;
        }
        
    $error $error1 $error2 $error3;
        if(
    $error 0)
        {
            
    $submit1 == "dontdislay";
            
    $submit2 == "submit2";
        }
        else{
            
    $submit1 == "submit1";
            
    $submit2 == "dontdisplay";
        }


    This sets the values so that if there are errors they are caught in the page and the form doesnt go to the next page. However, the problem I am having is when the use rhas actually completed the 3 form fields - this would mean $error = 0. However, despite this the form wont submit.

    Anyone got any ideas?

    Maybe there is a better way to deal with this? I want to avoid using javascript if possible.

    Thanks,
    Martin

  2. #2
    lean mean coding machine cosmo's Avatar
    Join Date
    Aug 2001
    Location
    the cosmos
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't see anything really wrong in your code.

    Edit:



    Are the two code snippets on the same page? Maybe the values in $submit1, $submit2 are not being passed properly.

    Last edited by cosmo; Dec 19, 2002 at 07:09.
    Luthfur R. - Web Developer
    PHP Lite.com - Professional PHP Scripts and Web Applications
    Calendar Express 2 - Web Based Calendar and Event Publishing System

  3. #3
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This answer will not make you happy...
    I think you have to rethink your survey completely; use one single php page for all "pages" (send page number as parameter). If $submit is set, then get the fields to validate, and if no error is found save the values and redirect to the next page (=same php script, next page number), otherwise display the same form again (and this time with error messages next to some of the elements). Inserting the values into the db is the easiest way to save them, but you can store them in hidden form fields too, and save them when the last page is submitted. However, the hidden fields can complicate things if the user wants to go back and change some answers.

  4. #4
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help.

    First off - the hidden form fields are working fine for me - if a user goes back and then resubmits then the form fields in the next page will contain the passed values, once they hit submit it is serving up a new call to the page and as such any passed variables will be parsed into the HTML as intended.

    I think I have a similar solution to what you refer to.

    I have 5 includes on 1 page. the submit1, submit2 correspond to each page. If a user hits submit 1 and there are no errors then submit1 = "submit1" and as such, the correct include is displayed.

    I am actually thinking of redoing this with javascript form validation instead. This would use alerts and wouldnt make it slower for people with slow connections.

    Anyone got any good javascript validators?


Bookmarks

Posting Permissions

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