Hi All,

I am posting here looking for a solution before giving up and going to something like AJAX, or some other non-PHP based solution.

We tend to use forms a lot, and one of our current forms, I am adding a rating request to our videos. The challenge, is we are continually adding new videos, and I need the users to rate and comment on the videos (which we give away free to teachers based on them giving us feedback).

I am redesigning the form in a 2 page setup, with their basic data on page one, and the video related data on page 2. The challenge is, previously, I used some Javascripting to test that they completed the required fields on the form, and its getting more complicated with all the videos we are adding, and now that I am making the form 'truly' dynamic with all the videos listed on the 2nd page with dynamic arrays for the ratings and comments, the Javascripting is totally beyond my capabilities.

I have it setup with sessions and setting the POST data to a SESSION data, which works fine for the first form and into the 2nd form, but if they do not completely fill out the 2nd form and I send them back (with a javascript:history.go(-1) ) then I get a "Webpage has expired" in IE and I'm dead in the water.

I tried to do as Leatherback suggested to me earlier and self post the pages to each other, but with this many pages into themselves, it got way to complicated.

So I am here, looking for any help.

I don't know if it exists, but I thought I would ask.

Regards,

Don

    One way to handle this is to do it all on the same page, but conditionally include different files based on some field name/value posted from the form, e.g.:

    <?php
    if(isset($_POST['form1_submit']))
    {
       require 'form1_processor.php';  // process input from Form #1
       if(count($errors) == 0)  // no errors
       {
          require 'form2.php';  // displays Form #2
       }
       else
       {
          require 'form1.php';  // re-displays Form #1
       }
    }
    elsif(isset($_POST['form2_submit']))
    {
       require 'form2_processor.php';  // processes data from Form #2
       if(count($errors) == 0)
       {
          header('Location: http://www.example.com/success.php'); // all done
          exit;
       }
       else
       {
          require 'form2.php';  // re-display Form #2
       }
    }
    else
    {
       require 'form1.php'  // display Form #1
    }
    ?>
    

    This way everything is still on one "page", but each main piece of the process is split into its own PHP file to help keep things to manageable file sizes and modularity. Also, the last-submitted form data is available in the $_POST array, so you can use it to prefill the form values when redisplaying after any errors were caught.

      Ok,

      I am following your logic (thanks for laying it out in an applicable fashion for me).

      One question. Where/how are you getting the (count($errors)) in this example?

      I don't mean to be simple minded, but I could take this several way.

      Thanks for taking the time to help me.

      Don

        No problem. It's just something I tend to do in my form validation routines. When I find an error, I add it to an array, usually cleverly called $errors. So in one of the form processor includes I might do something like:

        <?php
        $errors = array(); // initialize to empty array
        if(!isset($_POST['name']) or trim($_POST['name'] === '')
        {
           $errors[] = array('name' => '"Name" is a required field.');
        }
        // etc....
        if(count($errors) == 0)
        {
           // do whatever needs to be done with the data, such as database updates
           // if that action fails, then something like:
           $errors[] = array('db_error' => 'A database error occurred, please try again or contact blah blah blah....');
        }
        

        I can then use that array to output error messages and highlight fields on the form page (in its include file in this case):

        <?php
        // wherever I want to display the current error message(s):
        if(count($errors))
        {
           echo "<p class='error'>".implode("<br />\n", $errors)."</p>\n";
        }
        // to prefill a field if applicable and highlight it if there was an error:
        printf(
           "<input type='text' name='name' value='%s'%s />\n",
           (isset($_POST['name'])) ? htmlentities($_POST['name'], ENT_QUOTES) : '',
           (array_key_exists('name', $errors)) ? " style='background-color: #ffff99'" : ''
        );
        

          Ok,

          I am close.

          I have the form(s) working, and I am trying to implement your $errors code, and have some questions (if you do not mind).

          You suggested this snippet for the forms (NOTE my "include("errors.php"); // <-- my include $error page" addition.

          if(isset($_POST['form1_submit']))
          {
          require 'form1_processor.php'; // process input from Form #1
          include("errors.php"); // <-- my include $error page
          if(count($errors) == 0) // no errors
          {

          I took your advice, and made an include called errors.php

          $errors = array(); // initialize to empty array
          if(!isset($_POST['Title']) or trim($_POST['Title'] === ''))
          {
             $errors[] = array('Title' => '"Title" is a required field.');
          } 
          if(!isset($_POST['FirstName']) or trim($_POST['FirstName'] === ''))
          {
             $errors[] = array('FirstName' => '"First Name" is a required field.');
          }
          //etc......
          
          
          // wherever I want to display the current error message(s):
          if(count($errors))
          {
             echo "<p class='error'>".implode("<br />\n", $errors)."</p>\n";
          }
          // to prefill a field if applicable and highlight it if there was an error:
          printf(
             "<input type='text' name='name' value='%s'%s />\n",
             (isset($_POST['name'])) ? htmlentities($_POST['name'], ENT_QUOTES) : '',
             (array_key_exists('name', $errors)) ? " style='background-color: #ffff99'" : ''
          );
          

          Now, when I add this to you code, I get a series of just ...

          Array
          Array
          etc...

          for the missing data, so I know I either messed up your code, or applied at improperly, or both.

          Can you give me some guidance. Otherwise, I am close on this and your implementation (once I solve the error checking), will work for me.

          Thanks again,

          Dmacman

            Oops! My bad. (I was thinking of doing it one way with a multi-dim array, then realized that was overkill, but forgot to adjust the array assignment lines.) Just do the following when assigning an error to the array, instead of using the array() format:

            $errors['FirstName'] = '"First Name" is a required field.';
            

              Saaweet!

              This is working excellent. Thank you!

              I had an idea, and you may already do this.

              Couldn't I take your error code and make it a function, then echo the error next to the appropriate field?

              So for the $error code (this isn't working, but I have it to fine tune it).

              $errors = array(); // initialize to empty array
              
              function errors($string, $phrase){
              	if(!isset($string) or trim($string === ''))
              		{
              		   $errors[$string] = '"' . $string .'"' . $phrase . '';
              		}
              		return $errors[$string];
              	}
              
              $Zip = errors($_POST['Zip'], '"Zip" is a required field, please correct this entry.'); //example of function for below form
              
              

              Then next to each field I could echo the error (I did not apply the function to this, so help me out here if you could please).

              <input name="Zip" tabindex="22" size="10" maxlength="5" value="
              <?php echo $_SESSION['Zip']; ?>"/>
              <?php if(isset($errors['Zip'])) echo '<br>
              <font face="Arial" color="#FF0000" size="+1">' . $errors['Zip'] . '</font>'; ?>
              

              (NOTE: I need to change this to apply the above function, which I have not tested, or figured out how to apply yet.)

              Am I close here?

              This is really sweet the way you handled this as an all-inclusive form on one page (errors and all).

              I never would have coded this without your guidance, thanks!

              I appreciate your help,

              Don

                Never mind, I figured it out.

                For the function...

                $errors = array(); // initialize to empty array
                
                function errors($string, $phrase){
                	if(!isset($string) or trim($string === ''))
                		{
                		   $errors[$string] = $phrase ;
                		}
                		return $errors[$string];
                	}
                

                Then initalize all the errors messages...

                $zip = errors($_POST['Zip'], '"Zip" is a required field, please correct this entry.');
                

                And on the forms...

                <input name="Zip" tabindex="22" size="10" maxlength="5" value="<?php echo $_SESSION['Zip']; ?>"/>
                <?php echo '<br><font face="Arial" color="#FF0000" size="+1">' . $zip . '</font>'; ?>
                

                And if they miss a field, it will tell them from the function.

                Thanks NogDog.

                This has been an ongoing thorn for me, and I have been wrestling with this and Javascript for a year and a half. Now I can dump it and stay in PHP where I am happy!

                I owe ya one.

                Thanks,

                Don

                  Opps!

                  I resolved this too soon.

                  Now it passes your error test with errors ..

                     if(count($errors) == 0)  // no errors
                  

                  ..because of my function.

                  I also figured out, that you were using this.

                  printf(
                     "<input type='text' name='name' value='%s'%s />\n",
                     (isset($_POST['name'])) ? htmlentities($_POST['name'], ENT_QUOTES) : '',
                     (array_key_exists('name', $errors)) ? " style='background-color: #ffff99'" : ''
                  ); 
                  

                  for what I attempted to do with my function. Except, if I use this, I get a ..

                  Warning: array_key_exists() [function.array-key-exists]:

                  Is that because of the multi-dim array you had me remove?

                  If so, what do I need to fix this and get it working again?

                  Don

                    Write a Reply...