Forum Moderators: coopster

Message Too Old, No Replies

Attendance form with radio buttons

         

fastpager

8:20 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



I have a database generated attendance form. Each row has an attendee name and radio buttons to check absent or present. My problem is when the radio button is selected everbody is present or it echos everybody is absent. The radio button value that is checked in the first row, is passed to the other rows in the form. I have tried naming radio buttons to "attendee[]", but the word "array" is echoed to each row. I have tried while, for, & foreach loops but I get the same problem.
I am a newbee to php/html but I`m learning. How do I get the correct selected radio button value from each row of attendees in the form?
I am thankful for any help.


[size=1]
<?php

require "Include/Config.php";
require "Include/Functions.php";
require "Include/Header.php";

$sPageTitle = gettext("Church Event Editor");

// --------- initializing variables ------------

$sAction = $_POST['Action'];
$EventID = $_POST['EID'];
$per_ID = $_POST['PerID'];
$EvtName = $_POST['EName'];
$EvtDesc = $_POST['EDesc'];
$EvtDate = $_POST['EDate'];

//----------- process the action inputs to delete an attendee ---------

if ($sAction=='Drop'){
$dpeEventID=$_POST['DelPerEventID'];
$dpePerID=$_POST['DelPerID'];
$dpeSQL = "DELETE FROM event_attend
WHERE event_id=$dpeEventID && person_id=$dpePerID LIMIT 1";
RunQuery($dpeSQL);
$ShowAttendees = 1;

// ---------- construct the attendance form and submit button--------------
}?>

<form method="post" action="EditEventAttendees.php" name="AttendeeEditor">
<input type="hidden" name="EID" value="<?php echo $EventID ; ?>">
<table cellpadding="0" cellspacing="0" width="80%" align="center">
<caption>
<h3><?php echo gettext("Attendees for $EvtName"); ?></h3>
</caption>
<tr ><td colspan="5">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center"><span class="SmallText"><?php echo gettext("<strong>Description:</strong> <br>$EvtDesc") ?></span></td>
<td align="center"><span class="SmallText"><?php echo gettext("<strong>Date:</strong><br>$EvtDate") ?></span></td>
</tr>
</table>
</td>
</tr>
<tr><td colspan="5"></td></tr>
<tr>
<td colspan="6" align="right"><input type="submit" align="right" style="font-size: 12px;" name="submit" value="Record Attendance"></td><br>
</tr>
<tr><td colspan="6"></td></tr>
<tr class="TableHeader">
<td width="2%" style="padding-left:5px"><strong><?php echo gettext("Status"); ?></strong></td>
<td width="2%" style="padding-left:5px"><strong><?php echo gettext("ID#"); ?></strong></td>
<td width="15%" style="padding-left:10px"><strong><?php echo gettext("Name"); ?></strong></td>
<td width="15%" style="padding-left:10px"><strong><?php echo gettext("Email"); ?></strong></td>
<td width="15%" style="padding-left:10px"><strong><?php echo gettext("Home Phone"); ?></strong></td>
<td width="20%" align="right" style="padding-right:50px"><strong><?php echo gettext("Attendee Count"); ?></strong></td>

</tr>
<?php // ----------- query attendance information from database --------------

$sSQL = "SELECT * FROM event_attend WHERE event_id=$EventID ORDER BY person_id";
$rsOpps = RunQuery($sSQL);
$numAttRows = mysql_num_rows($rsOpps);
if($numAttRows!=0){
$sRowClass = "RowColorA";
for($na=0; $na<$numAttRows; $na++){
$attRow = mysql_fetch_array($rsOpps, MYSQL_BOTH);
extract($attRow);
$sSQL = "SELECT * FROM person_per WHERE per_ID = $person_id";
$perOpps = RunQuery($sSQL);
$perRow = mysql_fetch_array($perOpps, MYSQL_BOTH);
extract($perRow);
$sRowClass = AlternateRowStyle($sRowClass);
$aHomePhone = ExpandPhoneNumber($per_HomePhone,$per_Country,$dummy);

// ---------- process radio button results -----------------

$Present = $_POST['Present'];
$Absent = $_POST['Absent'];
$checked = $_POST['attendee'];
$Present = 'unchecked';
$Absent = 'unchecked';
if (isset($_POST['submit'])) {
if ($checked == 'Present');
$Present = 'checked';
}else if ($checked == 'Absent'); {
$Absent = 'checked';
foreach ($checked as $value);
echo $checked. " - " .$per_FirstName." " .$per_LastName; ?>&nbsp&nbsp<?php
}
// ---------- fill in the attendee form information and inputs -----------
?>
<tr class="<?php echo $sRowClass; ?>">
<td class="TextColumn"><?php echo ($checked); ?></td>
<td class="TextColumn"><?php echo ($per_ID); ?></td>
<td class="TextColumn"><?php echo FormatFullName($per_Title,$per_FirstName,$per_MiddleName,$per_LastName,$per_Suffix,3); ?></td>
<td class="TextColumn"><?php echo ($per_Email ? '<a href="mailto:'.$per_Email.'" title="Send Email">'.$per_Email.'</a>':'Not Available'); ?></td>
<td class="TextColumn"><?php echo ($aHomePhone ? $aHomePhone :'Not Available'); ?></td>

<td class="TextColumn" colspan="3" align="right">
<form method="POST" action="EditEventAttendees.php" name="Attendance">
<input type="hidden" name="DelPerID" value="<?php echo $per_ID; ?>">
<input type="hidden" name="DelPerEventID" value="<?php echo $EventID; ?>">
<input type="hidden" name="EID" value="<?php echo $EventID; ?>">
<input type="hidden" name="EName" value="<?php echo $EvtName; ?>">
<input type="hidden" name="EDesc" value="<?php echo $EvtDesc; ?>">
<input type="hidden" name="EDate" value="<?php echo $EvtDate; ?>">
<input type="hidden" name="attendee" value="<?php echo ($_POST['row_ID']); ?>">
<label><input type="radio" name="attendee" value="Present<?php echo($_POST['Present']); ?>" checked="checked">Present</label>
<label><input type="radio" name="attendee" value="Absent<?php echo($_POST['Absent']); ?>">Absent&nbsp&nbsp</label>
<input type="submit" style="font-size: 9px;" name="Action" value="<?php echo gettext("Drop"); ?>" onClick="return confirm('Are you sure you want to DELETE <?php echo $per_FirstName." " .$per_LastName; ?> from <?php echo $EvtName; ?>?')">

</form>
</td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="4" align="center"><?php echo gettext("No Attendees Assigned to Event") ?></td></tr>
<?php
}

?>
</table>

<?php require "Include/Footer.php"; ?>
[/size]

Tommybs

1:35 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



Hi,

Sorry I'm just struggling a bit to decipher your code for the attendance as it looks like there's a separate form for each attendee?

Just as a simple example though I've knocked this up:

<?php
if(isset($_POST['submit_form'])){
for($i = 1; $i < 10; $i ++){
echo "<p>Selected is ".$_POST['radio_but_'.$i]."</p>";
}
}
?>
<form action="radion.php" method="post">
<?php

for($i = 1; $i < 10; $i ++){
echo "<p>1 <input type=\"radio\" name=\"radio_but_$i\" value=\"1\" />&nbsp;&nbsp;";
echo "2 <input type=\"radio\" name=\"radio_but_$i\" value=\"2\" /></p>";
}
?>
<p><input type="submit" name="submit_form" value="submit" /></p>
</form>

I think you need to do something similar. what you could do is perform your query higher up in the page and store the number of attendees higher up in the script allowing you to use the count for the submit and the attendance form generation.

So something like this:( please note this is a very quick example missing echo statements etc. but is just an idea )

$q = mysql_query("select first_name from user order by first_name");
$c = mysql_num_rows($q);
if(isset($_POST['my_form'])){
for($j = 0; j < $c; j++){
echo "selected is ".$_POST['att_'.$j];
}
}
$i = 0;
<form name="attendance" method="post">
while($r = @mysql_fetch_array($q)){
Present<input type="radio" name="att_$i" value="present" />
absent<input type="radio" name="att_$i" value="absent" />
$i++;
}
<input type="submit" name="my_form" value="submit" />
</form>


Does that make sense? Is that what you want to achieve?

Matthew1980

6:43 pm on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Fastpager,

Welcome to the forum :)

I know as this was done yesterday, but I thought this addition was worth a mention ;)

// --------- initializing variables ------------

$sAction = strip_tags(mysql_real_escape_string($_POST['Action']));
$EventID = strip_tags(mysql_real_escape_string($_POST['EID']));
$per_ID = strip_tags(mysql_real_escape_string($_POST['PerID']));
$EvtName = strip_tags(mysql_real_escape_string($_POST['EName']));
$EvtDesc = strip_tags(mysql_real_escape_string($_POST['EDesc']));
$EvtDate = strip_tags(mysql_real_escape_string($_POST['EDate']));

Whenever you are using user submitted data into a sql statement, sanitise it, this will protect your valuable database an keep the contents safe. mysql_real_escape_string(); and strip_tags(); are about the best you can use to protect your data, and clean user input.

Cheers,
MRb

fastpager

6:58 pm on Apr 18, 2010 (gmt 0)

10+ Year Member



Tommybs - Thank you for your help and I`m working on your suggestions now. I will let you know how it turns out.

<-----------------------------------------------------------

Mathew1980 - Thank you. You are right. I have read about this but I`m just learning and I get in too big of a hurry. I do need to pay attention to secure coding.

Matthew1980

7:19 pm on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there fastpager,

Well, its a good habit to get into, and once you are more conversant with the language you can do a function for it to save retyping things over and over...

Good luck!

Cheers,
MRb