Jump to content

How would you validate a form like this?


karatekid36

Recommended Posts

This form will have infinite amount of users loaded into for when we need to take attendance at meetings.  How can you validate could for each of these radio groups when you are unsure of how many there will be because the amount of members can grow and shrink with time?  Once this is validated, I would like to Insert the user id, member id , and attendance into a table.  I do not yet understand how to validate this code and create an insert sql statement that will take into account that in week it may have 100 members but next time it may have 200, for example.  Any help would be great.  Thanks.

<?php
// Begin the page now.

$page_title = 'Brothers Of Psi Beta';


$query = mysql_query("SELECT * FROM brothers WHERE user_id='$user_id'");
$row = mysql_fetch_assoc($query);

if ($row['admin'] == 'admin'){
include ('./includes/header_admin.html');

} else {

include ('./includes/header.html'); 

}


// Page header.
echo '<h1 id="mainhead">Attendance</h1>';

// Make the query.
$query = "SELECT user_id, CONCAT_WS(', ', last_name, first_name) AS name FROM brothers WHERE status =\"Undergraduate\" ORDER BY last_name, first_name ASC";
$result = mysql_query ($query); // Run the query.

if  (!$result) {
    echo "The Query:
$query
Produced the error:
".mysql_error();
    exit;
}

// Table header.
echo '<form action="take_attend.php" method="post"><table align="center" cellspacing="0" cellpadding="3">
<tr>
<td align="left"><b>Brother</a></b></td>
<td align="left"><b>Attendance</a></b></td>

</tr>
';

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.

while ($row = mysql_fetch_assoc($result)) {//mysql_fetch_assoc is identical to mysql_fetch_array(resultresource, MYSQL_ASSOC but with less typing 
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['name'] . '</td>
	<td align="left">Present <input type ="radio" name="attend['.$row['user_id'].']" value="Present" />
	                      Excused <input type ="radio" name="attend['.$row['user_id'].']" value="Excused" />
		         Unexcused <input type ="radio" name="attend['.$row['user_id'].']" value="Unexcused" /></td>
</tr>
';
}


echo '</table>	<p><input type="submit" name="submit" value="Take attendance" /></p>
<input type="hidden" name="submitted" value="TRUE" />';

mysql_free_result ($result); // Free up the resources.	

mysql_close(); // Close the database connection.


include ('includes/footer.html'); // Include the HTML footer.
?>

Link to comment
Share on other sites

Create a table that stores the 'checked' values and that is linked to each user with some sort of id. Then, in your HTML code after your query, you could do something like this

 

while ($row = mysql_fetch_assoc($result)) {//mysql_fetch_assoc is identical to mysql_fetch_array(resultresource, MYSQL_ASSOC but with less typing 
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['name'] . '</td>
	<td align="left">Present <input type ="radio" name="attend['.$row['user_id'].']" value="Present" checked="' . if ( $row['checked'] == "Present") echo 'checked'; .'">>
	                      Excused <input type ="radio" name="attend['.$row['user_id'].']" value="Excused" checked=' . if ( $row['checked'] == "Excused") echo 'checked';/>
		         Unexcused <input type ="radio" name="attend['.$row['user_id'].']" value="Unexcused" checked=' . if ( $row['checked'] == "Unexcused") echo 'checked';/></td>
</tr>
';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.