Page 1 of 1

Process Forms Sequentially

Posted: Thu Jun 10, 2010 11:59 am
by pizzipie
I think my last post on this subject was poorly done on my part. Too complicated for basically a simple question.

This is the situation in a nutshell w/o all the code etc.

I am trying to show a series of forms, 5 in this case, and process the related data by calling a function. ie:

foreach(table $key as $value) {
call function(x,y,z) {
show form for table x
return()
}
process $_POST data
}


Problem is as soon as I switch to the page with the function calls the browser shows all 5 forms at once.

How can I process the information from each form one at a time ?

Thanks, Rick P

Re: Process Forms Sequentially

Posted: Thu Jun 10, 2010 4:15 pm
by JakeJ
Are you wanting to display the forms based on user interaction?

You can display the form that is submitted by testing to see which button is pressed and then just process that form and set a condition so that only that form is displayed after processing.

Perhaps I'm not really clear on what you're trying to accomplish.

Re: Process Forms Sequentially

Posted: Thu Jun 10, 2010 4:59 pm
by pizzipie
Thanks JakeJ,

My address database is set up as follows:

Tables: 'people', 'home', 'phone', 'work', 'other'
'people' is the 'base' table showing only First, Last, Display Names, Home Phone
and Email info.

Each of the other tables show a little more data about the person.

If I want to add a person to the database I call on the form that shows
the 'people' fields fill that out and then to input any additional data I cycle through the other
table forms adding data as desired.

To accomplish this, the forms for all the other tables are accessed by passing Table, and Name_ID through a single function that shows the form
for that table.

The problem is that the browser shows all 5 forms at once rather than, for instance,
the 'home' form which I would submit on and then the 'phone' form for which I would submit on etc., etc.

I used to program in 'C' so I like functions to do repetitive chores. I am trying to avoid having a page for each form if this is even necessary.

You allude to a button being pressed but there is no button press. Only a function call like this.

foreach($table as $value) {
ShowTableForm(x,y,etc.)
}

Thanks for your prompt help. I hope this explains things a little better.

RP.

Re: Process Forms Sequentially

Posted: Thu Jun 10, 2010 7:25 pm
by JakeJ
How are you submitting without a button? Just hitting the enter key?

It sounds like you need some sort of conditional statement to decide which form to show.

Each form can be wrapped in:

Code: Select all

If (isset($some_variable) && $some_variable == $some_value) {
 //form code
}
Also keep in mind that functions are great but they don't solve all of your problems.

Hopefully I've understood your problem correctly.

Re: Process Forms Sequentially

Posted: Fri Jun 11, 2010 12:49 pm
by pizzipie
Here is the code and a .jpg file showing the results.

By the way I tried to upload the code via file but it would not accept file..
Can you tell me what this is about?

R

Code: Select all

<?php // 9 Jun 2010
session_start();


  /* Program:      AssignIdToTables.php
   *
   * Description:  After initial addition of Data in the 'people' table (NewAddress.php)

   *               assigns the people.Name_ID to all the other tables, 'home', 'other', 'phone', 'work'

   *               then cycles through the tables allowing Data entry into each table.
   *
   * Problem:      When cycling through forms all forms show up on one page.
   *
   * How To:       Cycle and enter data to each table one at a time.               

   */

?>


<html>

<head><title>Assign Id's and data to tables</title></head>

<body>


<?php


        
 require_once("AddressHeadings.inc");                               

 require_once("functions.inc");
 require_once("functions-address.inc");
  
 
  $NameId=$_SESSION['$InsertId'];   // from NewAddress.php

  $cxn = mysqli_connect($hostName, $username, $password, $databaseName);
     if (!$cxn) {
          die('Could not connect: ' . mysqli_error($cxn));
     }
 
      foreach($table as $key => $value) {                                        // $table from AddressHeadings.inc 
          $labels[]=array_combine($FieldNames[$value], $FormNames[$value]);       // Table heading
     }
                                                    
     $i=0;
     foreach($TablesToId as $value) {                    // $TablesToId, $FieldNames from AddressHeadings.inc
         InsertTableID($cxn, $NameId, $TablesToId[$i++], $FieldNames[$value] );  // cycle thru tables assigning $Name_Id
     }     

      $i=0;
      foreach($TablesToId as $value) {                  // cycle thru Data Entry Form adding data to tables
          $x=$TablesToId[$i];
                                  
          ShowTableForm($cxn, $NameId, $TablesToId[$i++], $labels[$x]);  
    }  
   
    ShowTableForm($cxn, $NameId, $TablesToId[$i++], $labels[$x]); 
    
    exit();    // to be continued                        
                                        
                                        
  /*  =====================================  Function  InsertTableID()  =============================== */
 
     function InsertTableID ($cxn, $Id, $fields, $values) {
      
          $query ="REPLACE INTO $fields SET Name_ID=$Id;";

          $result= mysqli_query($cxn, $query);         
          if((mysqli_error($cxn))) {
                echo "<p style='font-size: large; font-weight: bold; text-align: center'>
                     <br> <br><br><br>Error occured:" . mysqli_error($cxn);
           }    
      } 
 
  /*  ======================================== Function  InsertTableData()  ============================== */
 
        function InsertTableData($Id, $table, $fields, $values) {
       
                $query= "REPLACE INTO `$table` (";
                $query.= implode(",", $fields);
                $query.= ") VALUES ('";
                $query.= implode("','", $values);
                $query.="');"; 
                return($query);                     
        }
      
 
 
   /*  =========================  Function  ShowTableForm - Add/Replace Table Contents  =================== */
 
     function ShowTableForm($cxn, $NameId, $table, $array) {

          echo "<div style='text-align: center'>

             <h2>Creating New Address </h2>\n";

          echo "<p style='font-size: medium; font-weight: bold'> 

             Address must be a unique.<br> </p><hr />  ";
 
         /* ================================  Form  ==========================================*/
         
          echo "<form  action='' method='POST'>";             
          echo"<p><table align='center'>\n";
       


     foreach($array as $field=>$label) {


          if($field=="Name_ID")
                                   
               echo "<tr>

                     <td style='text-align: right; 

                         font-weight: bold'> </td>

                     <td><input type='hidden' name='$field' 

                         value='$NameId' size='65'      

                         maxlength='65'> 

                     </td></tr>  "; 
          else              
          
          if($field=="Notes")
               
                echo "<tr><td style='text-align: right;
                              font-weight: bold'>$label</td>
                          <td><textarea name='$field'
                               rows='4' cols='65' wrap='wrap'> $Records[$field]
                               </textarea>
                          </td></tr> "; 

          else      

                echo "<tr>

                        <td style='text-align: right; 

                          font-weight: bold'>$label</td>

                        <td><input type='text' name='$field' 

                        value='$Records[$field]' size='65'      

                        maxlength='65'> 

                        </td></tr>  ";

  }  // foreach($array)


  echo "<tr><td>&nbsp;</td>

         <td style='text-align: left'>

           <input type='submit'  name='operation'  value='Create Address'>";     // submit named 'operation' put in $_POST
     echo "<input type='submit'  name='operation' value='Never Mind'>";           

     echo "</td></tr></table>

        </div>

        </form>"; 
         
          /* ========================================  End Form  ========================== */
                  

        if($_POST['operation']=='Create Address') {      // process submitted data
                   
                $Post=$_POST;
                
                array_pop($Post); // make up for no IDno field and having to insert it manually below
                
                
                        foreach ($Post as $field => $value) {   // get input data from form
                          
                                $Data[$field]=$Post[$field];
                                $fields[]=$field;
                                $values[]=$Data[$field];  
                        }  
                                $query=InsertTableData($NameId, $table, $fields, $values); // echo $NameId, $query; exit();                  
                                $result= mysqli_query($cxn, $query);
                                
                                 if((mysqli_error($cxn))) {
                                         echo "<p style='font-size: large; font-weight: bold; text-align: center'>
                                           <br> <br><br><br>Error occured: " . mysqli_error($cxn);     
                                           
                                         echo" <br> <br>     
                                           <a href='ExitProgram.php'>Exit or </a><br>
                                           <a href='AddressFrontEnd.php'>Start Over  </a></p>";                                              
                                  } 
                                  
                                 else {                                   

                                         echo "<p style='font-size: medium; font-weight: bold; text-align: center'>
                                           Account Created Successfully<br><br>     
                                           <a href='ExitProgram.php'>Exit or </a><br>
                                           <a href='AddressFrontEnd.php'>Start Over  </a></p>"; 
                                            
                                  }       
                                     
                         
       } // if($_POST['operation']
         
          else 
                if($_POST['operation']=='Never Mind') {
                
                   echo "<p style='font-size: medium; font-weight: bold; text-align: center'>
                              <a href='ExitProgram.php'>Exit or </a><br>
                              <a href='AddressConnect.php'>Start Over  </a></p> ";               
                
                }  // If Never Mind

              return($Id[0]);
                
        }    // ShowTableForm()
            
          
?>

 

Re: Process Forms Sequentially

Posted: Fri Jun 11, 2010 5:48 pm
by pizzipie
JakeJ

I've cobbled together a simple example program that exhibits the problem I'm having
without having to follow my programming as submitted this morning.

I cycle 3 times and get three forms displayed all at once.

What I want is to have each form in sequence to fill out and then go to the next form.

Code: Select all

<?php
     for($i=0; $i<3; $i++) {
?>
<form action="" method="post">
First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text" name="mobile"><br>
Fax: <input type="text" name="fax"><br>
E-mail: <input type="text" name="email"><br>
<input type="Submit">
</form>

<?php
$username="rick";
$password="rick";
$database="contacts";


$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];

//$cxn=mysql_connect(localhost,$username,$password);
//@mysql_select_db($database) or die( "Unable to select database". mysql_error($cxn);

$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";

echo $query,"<br>";

}
//mysql_query($query);

Re: Process Forms Sequentially

Posted: Sat Jun 12, 2010 2:08 am
by JakeJ
The main problem is this line: for($i=0; $i<3; $i++) {

It's telling your code to execute 3 times, so you're getting 3 copies of the same form. The same thing applies to your previous code example, you're executing a loop and so it reproduces the same thing 3 times.

What you want to do is have a self submitting form. The action of the form should be $_SERVER['PHP_SELF'].

Then you want to give each of your submit buttons the same name ('submit' would be good), but different id's.

Then, when the page is processed, determine which button is pressed. Say your buttons values are 1, 2 and 3.

Code: Select all

<?php
If (!isset($_POST['submit'])) {
   //execute code for first form
}
Else {
   If ($_POST['submit'] ==1) {
     //do data base inserts, updates etc.
    //execute code for second form
    }
   If($_POST['submit'] ==2) {
    //do database inserts, updates, etc.
    //execute code for 3rd form
   }
   If ($_POST['submit'] == 3) {
    //do database inserts, update, etc.
    echo "All forms submitted"; //Or whatever you want here
   }
}
I'm assuming of course that you're only going to have 3 forms. If you don't know the number of forms you'll have it gets more complicated. Also, instead of those If statements, you could use a switch, but there's no functional difference.

**PLEASE NOTE** I did that code off the top of my head and did not test anything.

With PHP there are always lots of ways to do any given thing. I know you like functions, but I'm not sure putting this all in to a function would make sense in this case. You could put it in to a Class though, that might make sense. But if you only have 3 forms, you might as well keep it simple.

I hope this helps.

Re: Process Forms Sequentially

Posted: Tue Jun 15, 2010 1:18 pm
by pizzipie
The problem with your approach is that you end up with a bunch of submit buttons that have to be pressed to select the next table. I was hoping to have a seamless data entry sequence from one table to the next. When one is filled out and submitted the next one appears and so on. Is there a way to do that in Javascript.

Thanks,

R

Re: Process Forms Sequentially

Posted: Tue Jun 15, 2010 2:18 pm
by JakeJ
Perhaps I'm still confused about what you are trying to accomplish.

In my current application, I have a 13 part form not including registration and logging on.

When submit is pressed on the form, it processes it and loads the next form. Load, submit, process, load new form....... and so on.

Are you trying to accomplish something different than that?

Re: Process Forms Sequentially

Posted: Tue Jun 15, 2010 4:48 pm
by pizzipie
No I am not. I'm trying to do just that, but using functions instead of having to write all those pages of code.
Also, the functions I create can be used in any other application by simply changing variables to each call. My include files set up table fields and appropriate labels in arrays as so: <?php
// tables in address database
$table=array('people'=>'people', 'home'=>'home', 'other'=>'other', 'phone'=>'phone', 'work'=>'work');
$TablesToId=array('home', 'other', 'phone', 'work'); // Set initial Name_ID when adding an address

// Column heading we wish to show in our forms (not table field names)
$colhead=array(
'people'=>array('ID', 'First Name','Last Name', 'Display Name', 'Home Phone', 'Email', 'Email'),
'home'=>array('ID', 'Address', 'City', 'State', 'Zip'),
'phone'=>array('ID','Work Phone', 'FAX', 'Cell 1', 'Cell 2'),
'other'=>array('ID', 'Notes'),
'work'=>array('ID', 'Address', 'City', 'State', 'Zip', 'Job Title ', 'Department', 'Organization', 'Web 1', 'Web 2'));
?>


So in my current application, the following would happen:

Start in AddressFrontEnd.php; href to AddNewAddress.php
fill out 'people' form => submit; assign and pass $_SESSION['ID'] to AssignToTables.php
call to function InsertTableID(x, y, z, $_SESSION['ID']) sets $Name_ID in each other table, 'home','phone, 'work', and 'other'
foreach-looping-call to function ShowTableForm(x, y, z) which will access each form which has already had it's $Name_ID set to the same as 'people'
and proceed to REPLACE data into the form.

This would work fine except that the browser displays all the forms for each table at the same time !!

I'm thinking this must be really simple or it can't be done with the languages I'm using. I am really too new to HTML, PHP, Javascript, etc
to really know how things work re: the server-client relationship. (I've read somewhere PHP is client oriented, Javascript is server oriented)

I really appreciate your help in this and in staying with me.