Page 1 of 1

Cycle Thru Form for Different Tables - All forms on one page

Posted: Wed Jun 09, 2010 4:49 pm
by pizzipie
I am writing an address data entry program. I have a function where I cycle through it; each time using a different table to enter data into. At present all the data entry forms show up simultaneously on one page. I need help on getting the program to show each form one at a time and processing the data therefrom. I don't have much experience with this client server programming so please explain to an idiot.

Thanks in advance RP. Code Below.

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());
     }
 
      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]);  
    }  
    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()
            
          
?>