CSC220 Self Reloading Php Form

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 23:30, 5 October 2010 (UTC)


<HTML>

<HEAD>
<TITLE>CSC220 -- ReloadForm</TITLE>
<meta http-equiv="Pragma" content="no-cache">
</HEAD>

<BODY>
<font size="small">(<a href="reloadform.txt">source</a>)</font><br>
<h1>Example of a form page in Php that calls itself</h1>
<?php

//--------------------------------------------------------------------
// testPage: checks to see if all three fields are given.  If one or
//           more missing, displays apropriate message.
//           
//--------------------------------------------------------------------
function testPage( $name, $age, $occupation ) {

   if ( !empty( $name ) && !empty( $age ) && !empty( $occupation ) ) {
     displayFinalPage( $name, $age, $occupation );
     return 3;
   }

   $params = 0;

   if ( !empty( $name ) ) {
     printf( "<h2>Hello %s</h2>\n", $name );
     $params++;
   }
   else
     printf( "<h2>Dear mysterious visitor...</h2>\n" );

   if ( !empty( $age ) ) {
     printf ("<P>You were born in %d...", 2010-$age );
     $params++;
   }
   else
     printf ("<P>We need your age to continue this application...\n" );

   if ( !empty( $occupation ) ) {
     printf ("<P>We have recorded that your occupation is %s\n", $occupation );
     $params++;
   }
   else
     printf( "<P>We are missing your occupation...\n" );

   return $params;
 }

//--------------------------------------------------------------------
// displayFinalpage: displays the final message of congratulations
//--------------------------------------------------------------------
function displayFinalPage( $name, $age, $occupation ) {
  print "<h1>Dear $name</h1>
           Thank you for providing us with the information we requested.
           We have recorded your age of $age, and your occupation ($occupation).
           <P>
           Have a good day!";
}

//--------------------------------------------------------------------
// displayRequestArray: debug function to show contents of $_REQUEST
//--------------------------------------------------------------------
function displayRequestArray() {
  print "<table width=\"50%\"><tr bgcolor=\"\#cc99cc\"><td><pre>\$_REQUEST = ";
  print_r( $_REQUEST );
  print "</pre></td></tr></table><P><P>\n";
}

//--- get the form parameters ---
$name = $_REQUEST['name'];
$age  = $_REQUEST['age'];
$occu = $_REQUEST['occupation'];

//--- did we get all 3 params? ---
$noParamsSet = testPage(  $name, $age, $occu );

if ( $noParamsSet < 3 ) {
  printf( '<FORM ACTION="reloadform.php" METHOD="POST">
         <P>
         Name: <INPUT TYPE="text" NAME="name" VALUE="%s" SIZE="40">
         <P>
         Age: <INPUT TYPE="text" NAME="age" VALUE="%s" SIZE="5">
         <P>
         Occupation: <INPUT TYPE="text" NAME="occupation" VALUE="%s" SIZE="40">
         <P>
         <INPUT TYPE="SUBMIT" VALUE="Send">
         </FORM>', $name, $age, $occu );
}


//--- for debugging ---
displayRequestArray();

?>



<P>
</BODY>
</HTML>