Page 1 of 1

Automating php form handling

Posted: Sun Feb 15, 2004 11:05 pm
by Matta
I'm new to PHP/MySQL so please help! I posted this before viewtopic.php?t=17732&highlight=, and though the feedback was good, I still need help.

I have a bunch of pages with long forms, with lots of input types-text, radios, textareas, and the debugging process has become overwhelming.
What I need to happen is to make sure that
1. Every field on the page is filled out-if not, a list of incorrect fields is displayed, and any radio boxes that were checked still are
2. The fields are queried to a table in a database. (The table has the same column names as the field names)

Here's the code. I gave up trying to type in all the field names and variable names, but that inconsistency is exactly what I'm trying to eliminate:

Code: Select all

<?php # Script 7.2 - loggedin.php
require_once ('../mysql_connect.php');
$page_title = 'Sight Vocabulary';


if (isset($_POST['submit'])) { // Handle the form.
	
	
	$message = NULL; // Create an empty new variable.
		
	// Create a function for escaping the data.
	function escape_data ($data) {
		global $dbc; // Need the connection.
		if (ini_get('magic_quotes_gpc')) {
			$data = stripslashes($data);
		}
		return mysql_real_escape_string($data, $dbc);
	} // End of function.
	
	
	// Make sure every field is filled out---way too bloated.
	if (empty($_POST['morning']) ||empty($_POST['tired']) ||empty($_POST['shiny']) ||empty($_POST['old']) ||empty($_POST['trade']) ||empty($_POST['promises']) ||empty($_POST['pieces']) ||empty($_POST['picked']) ||empty($_POST['push']) ||empty($_POST['through']) ||empty($_POST['begins']) ||empty($_POST['food']) || empty($_POST['light']) || empty($_POST['ends']) || empty($_POST['clue']) || empty($_POST['breathe']) || empty($_POST['insect']) || empty($_POST['weather']) || empty($_POST['noticed']) || empty($_POST['money']) || empty($_POST['higher_sight_vocab'])) {
		$var1 = FALSE;
		$message .= '<p>You must complete every part of the assessment.</p>';
  
	} else {
		$var1 = escape_data($_POST['morning']);
		$var2 = escape_data($_POST['tired']);
		$var3 = escape_data($_POST['shiny']);
		$var4 = escape_data($_POST['old']);
		$var5 = escape_data($_POST['trade']);
		$var6 = escape_data($_POST['promise']);
		$var7 = escape_data($_POST['pieces']);
		$var8 = escape_data($_POST['picked']);
		$var9 = escape_data($_POST['push']);
		$var10 = escape_data($_POST['through']);
		$var11 = escape_data($_POST['begins']);
		$var12 = escape_data($_POST['food']);
		$var13 = escape_data($_POST['light']);
		$var14 = escape_data($_POST['ends']);
		$var15 = escape_data($_POST['clue']);
		$var16 = escape_data($_POST['breathe']);
		$var17 = escape_data($_POST['insects']);
		$var18 = escape_data($_POST['weather']);
		$var19 = escape_data($_POST['noticed']);
		$var20 = escape_data($_POST['money']);
		$var21 = escape_data($_POST['higher_sight_vocab']);
		
		
	}

//Below is what I'm trying to do to automate this thing.
	
/*$required=array('morning', 'morning_incorrect', 'tired', 'tired_incorrect', 'shiny', 'shiny_incorrect', 'old', 'old_incorrect', 'trade', 'trade_incorrect', 'promise', 'promise_incorrect', 'pieces', 'pieces_incorrect', 'picked', 'picked_incorrect', 'push', 'push_incorrect', 'through', 'through_incorrect', 'begins', 'begins_incorrect', 'food', 'food_incorrect', 'light', 'light_incorrect', 'ends', 'ends_incorrect', 'clue', 'clue_incorrect', 'breathe', 'breathe_incorrect', 'insects', 'insects_incorrect', 'weather', 'weather_incorrect', 'noticed', 'noticed_incorrect', 'money', 'money_incorrect');

function field_check($required)
{ 
foreach($required as $q)
	{ 
   if(empty($_POST[$q]))
		{ 
  	 	return 'Error: The following field was not filled in: $q'; 
   		} 

	} 
}
*/	

	
	if ($var1 &&  $var2 &&  $var3 &&  $var4 &&  $var5 &&  $var6 &&  $var7 &&  $var8 &&  $var9 &&  $var10 &&  $var11 &&  $var12 &&  $var13 &&  $var14 &&  $var15 &&  $var16 &&  $var17 &&  $var18 &&  $var19 &&  $var20 &&  $var21) { 
// If everything's OK.
		
		$u = addslashes($_COOKIE['user_id']);
		
		// Make the query
		$query = "INSERT INTO second_grade_reading (user_id, morning, tired, shiny, old, trade, promise, pieces, picked, push, through, begins, food, light, ends, clue, breathe, insects, weather, noticed, money, higher_level) VALUES ('$u','$var1', '$var2', '$var3', '$var4', '$var5', '$var6', '$var7', '$var8', '$var9', '$var10', '$var11', '$var12', '$var13', '$var14', '$var15', '$var16', '$var17', '$var18', '$var19', '$var20', '$var21')";
		
		//echo $query; exit;
		
		

		$result = @mysql_query ($query); // Run the query.
			
			
		if ($result) { // If it ran OK.
			
				// Write the feedback for the page.
				include ('templates/header.inc');
				$page_title = 'Entry Recorded';
				echo "<p><b><a href="sv_3rd_grade.php" class="content">Continue</a>";
include ('templates/footer.inc');
				 
				exit(); // Quit the script.
				
		} else { // If it did not run OK.
				$message = '<p>Your answers could not be recorded due to a system error. We apologize for any inconvenience.</p><p>' . mysql_error() . '</p>'; 
		}		
		
	} 
		mysql_close($dbc); // Close the database connection.

} 


include ('templates/header.inc');

// Print the error message if there is one.
if (isset($message)) {
	echo '<font color="red">', $message, '</font>';
}

// Print the page.



?>

<p>Emily was presented with a series of 
        graded word lists, beginning at the second grade level. Evaluate Emily&#8217;s 
        reading of the graded word lists. The criteria for scoring the word lists 
        have been provided.</p>
       <p> Print out a copy of each word list by going to the FILE menu and selecting 
        PRINT. Click on the audio button to hear Emily read the lists. Place a 
        "C" (correct) in the "Identified Automatically" 
        column for words that Emily identified correctly without having to sound 
        them out. Place a "C" in the "Identified" column 
        for words that Emily identified correctly but needed to sound out. For 
        incorrect responses, write the incorrect attempt in the "Incorrect" 
        column. </p>
       <p> For each word list, determine Emily's performance and decide if 
        you would have her do any further reading. Click the ADD TO RECORD button 
        after each decision.</p>
      <p> <a href="../audio/2ndgrade_list.mp3" class="content">Click here to hear Emily read a 
        second-grade word list</a></p>
      <p><span class="sub_section_header">GRADE LEVEL TWO WORD LIST:</span>
      </p>
      <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
     <table width="75%" border="1">
        <tr>
          <td>&nbsp;</td>
            <td>Identified Automatically</td>
            <td> Identified</td><td>Incorrect</td>
        </tr>
        <tr>
          <td>1. morning&nbsp;</td>
          <td><input type="radio" name="morning" value="identified_automatically"</td>
<td><input type="radio" name="morning" value="identified"></td><td><input type="text" name="morning_incorrect"></td>
        </tr>
        <tr>
          <td>2. tired</td>
          <td><input type="radio" name="tired" value="identified_automatically"</td>
<td><input type="radio" name="tired" value="identified"></td><td><input type="text" name="tired_incorrect"></td>
        </tr>
        <tr>
          <td>3. shiny</td>
          <td><input type="radio" name="shiny" value="identified_automatically"</td>
<td><input type="radio" name="shiny" value="identified"></td><td><input type="text" name="shiny_incorrect"></td>
        </tr>
        <tr>
          <td>4. old </td>
          <td><input type="radio" name="old" value="identified_automatically"</td>
<td><input type="radio" name="old" value="identified"></td><td><input type="text" name="old_incorrect"></td>
        </tr>
        <tr>
          <td> 5. trade</td>
          <td><input type="radio" name="trade" value="identified_automatically"</td>
<td><input type="radio" name="trade" value="identified"></td><td><input type="text" name="trade_incorrect"></td>
        </tr>
        <tr>
          <td>6. promise</td>
          <td><input type="radio" name="promise" value="identified_automatically"</td>
<td><input type="radio" name="promise" value="identified"></td><td><input type="text" name="promise_incorrect"></td>
        </tr>
        <tr>
          <td>7. pieces</td>
          <td><input type="radio" name="pieces" value="identified_automatically"</td>
<td><input type="radio" name="pieces" value="identified"></td><td><input type="text" name="pieces_incorrect"></td>
        </tr>
        <tr>
          <td>8. picked</td>
          <td><input type="radio" name="picked" value="identified_automatically"</td>
<td><input type="radio" name="picked" value="identified"></td><td><input type="text" name="picked_incorrect"></td>
        </tr>
        <tr>
          <td>9. push</td>
          <td><input type="radio" name="push" value="identified_automatically"</td>
<td><input type="radio" name="push" value="identified"></td><td><input type="text" name="push_incorrect"></td>
        </tr>
        <tr>
          <td>10. through</td>
          <td><input type="radio" name="through" value="identified_automatically"</td>
<td><input type="radio" name="through" value="identified"></td><td><input type="text" name="through_incorrect"></td>
        </tr>
        <tr>
          <td>11. begins</td>
          <td><input type="radio" name="begins" value="identified_automatically"</td>
<td><input type="radio" name="begins" value="identified"></td><td><input type="text" name="begins_incorrect"></td>
        </tr>
        <tr>
          <td>12. food</td>
          <td><input type="radio" name="food" value="identified_automatically"</td>
<td><input type="radio" name="food" value="identified"></td><td><input type="text" name="food_incorrect"></td>
        </tr>
        <tr>
          <td>13. light</td>
          <td><input type="radio" name="light" value="identified_automatically"</td>
<td><input type="radio" name="light" value="identified"></td><td><input type="text" name="light_incorrect"></td>
        </tr>
        <tr>
          <td>14. ends</td>
          <td><input type="radio" name="ends" value="identified_automatically"</td>
<td><input type="radio" name="ends" value="identified"></td><td><input type="text" name="ends_incorrect"></td>
        </tr>
        <tr>
          <td>15. clue</td>
          <td><input type="radio" name="clue" value="identified_automatically"</td>
<td><input type="radio" name="clue" value="identified"></td><td><input type="text" name="clue_incorrect"></td>
        </tr>
        <tr>
          <td>16. breathe</td>
          <td><input type="radio" name="breathe" value="identified_automatically"</td>
<td><input type="radio" name="breathe" value="identified"></td><td><input type="text" name="breathe_incorrect"></td>
        </tr>
        <tr>
          <td>17. insects</td>
          <td><input type="radio" name="insects" value="identified_automatically"</td>
<td><input type="radio" name="insects" value="identified"></td><td><input type="text" name="insects_incorrect"></td>
        </tr>
        <tr>
          <td>18. weather</td>
          <td><input type="radio" name="weather" value="identified_automatically"</td>
<td><input type="radio" name="weather" value="identified"></td><td><input type="text" name="weather_incorrect"></td>
        </tr>
        <tr>
          <td>19. noticed</td>
          <td><input type="radio" name="noticed" value="identified_automatically"</td>
<td><input type="radio" name="noticed" value="identified"></td><td><input type="text" name="noticed_incorrect"></td>
        </tr>
		<tr>
          <td>20. money</td>
          <td><input type="radio" name="money" value="identified_automatically"</td>
<td><input type="radio" name="money" value="identified"></td><td><input type="text" name="money_incorrect"></td>
        </tr>
      </table>
	  
	 
       <table width="75%" border="1">
        <tr>
          <td>Total Correct Automatic</td>
		  <td>_/20=_%</td>
		  </tr>
		  <tr>
          <td>Total Correct Identified</td>
		  <td>_/20=_%</td>
		  </tr>
		  <tr>
          <td>Total Number Correct</td>
		  <td>_/20=_%</td>
		  </tr>
      </table>
      <p><span class="sub_section_header">CRITERIA FOR LEVELS</span></p>
      <table width="75%" border="1">
        <tr>
          <td>Independent</td>
          <td>Instructional</td>
          <td>Frustration</td>
        </tr>
       <tr>
          <td>18-20</td>
          <td>14-17</td>
          <td>below 14</td>
        </tr>
        <tr>
          <td>90-100%</td>
          <td>70-85%</td>
          <td>below 70%</td>
        </tr>
      </table>
      <p> <br>
        Do you want to administer a higher level word list? </p>
        <p> 
          <input type="radio" name="higher_sight_vocab" value="yes">
          YES &nbsp;&nbsp; 
          <input type="radio" name="higher_sight_vocab" value="no">
          NO </p>
        <p> 
          <input type="submit" name="submit" value="Add to Record">
         </p>
      </form>
      
 
<?
include ('templates/footer.inc'); // Include the HTML footer.
?>
?>