please help with printing values of check boxes.
Posted: Tue Nov 21, 2006 7:06 am
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi guys.
I'm starting a little project that helps university lecturers book lecture rooms for their lessons. I'm struggling with something quite simple and some guys here said they would help so I'm posting some code.
It's basically, a simple request a room form. The problem is there are two fields (weeks, and facilities) that use a series of checkboxes.
I am trying to create an array that stores the values of each checkbox, whether it has been checked on not and then print each week and whether it is requested or not based on whether the box was checked. I'm doing this by looping throuhg the array but it isnt printing anything out at the moment.
Please could someone look at the code and try running it and suggest someimprovements?
thanks alot for any help.Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
Request Room
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="style.css" type="text/css"/>
<title>Request a Room</title>
</head>
<body>
<?php
require ('db_login2.php');
require('formhelpers.php');
if ($_POST['_submit_check']){
if ($form_errors = validate_form()){
show_form($errors = $form_errors);
}
else
{
process_form();
}
}
else{
show_form();
}
function show_form($errors = ''){
if ($errors){
echo "Please correct these errors: <ul><li>";
echo implode("</li><li>", $errors);
echo "</li></ul>";
}
$priorities =array('high' => 'high','normal' => 'normal','low' => 'low');
$modules = array('Team Projects' => 'Team Projects', 'Programming1' => 'Programming1','Operating Systems' => 'Operating Systems');
$rooms = array('Any' => 'Any','CC013' => 'CC013','CC012' => 'CC012','James France' => 'James France');
$area = array('Any' => 'Any','East' => 'East','West' => 'West','Central' => 'Central', 'Village' => 'Village','Hollywell' => 'Hollywell');
$day = array('Mon' => 'Mon', 'Tue' => 'Tue','Wed' => 'Wed', 'Thu' => 'Thu', 'Fri' => 'Fri');
$times = array('9' => '9', '10' => '10','11' => '11','12' => '12','13' => '13','14' => '14','15' => '15','16' => '16','17' => '17');
?>
<table border = 1>
<tr>
<td>
<?php echo "<form method ='POST' action ='".$_SERVER['PHP_SELF']."'>";?>
Priority:    <?php input_select('priority','normal', $priorities); ?> <br />
Module Code:    <?php input_select('modules', '', $modules); ?> <br />
Room Code:    <?php input_select('roomcode', '', $rooms); ?> <br />
Area:    <?php input_select('area', '', $area); ?> <br />
No. of Students:     <?php input_text('students', '',3); ?><br />
Day:     <?php input_select('day','',$day); ?><br />
Weeks:<br />    <?php input_radiocheck('checkbox', 'weeks[]', 'Full Semester', '', 'checked'); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week1', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week2', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week3', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week4', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week5', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week6', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week7', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week8', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week9', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week10', '', ''); ?> <br>
    <?php input_radiocheck('checkbox', 'weeks[]', 'week11', '', ''); ?> <br>
Start Time:    <?php input_select('start', '', $times); ?> <br />
End Time:    <?php input_select('end', $times[1], $times); ?> </td>
<td><p>Facilities:<br />
    
<?php input_radiocheck('checkbox', 'blackboard', 'blackboard', '', ''); ?>
<br>
    
<?php input_radiocheck('checkbox', 'whiteboard', 'whiteboard', '', ''); ?>
<br>
    
<?php input_radiocheck('checkbox', 'computer point', 'computer_point', '', ''); ?>
<br>
    
<?php input_radiocheck('checkbox', 'network', 'network', '', ''); ?>
<br>
    
<?php input_radiocheck('checkbox', 'projector', 'projector', '', ''); ?>
<br>
    
<?php input_radiocheck('checkbox', 'microphone', 'microphone', '', ''); ?>
<br>
    
<?php input_radiocheck('checkbox', 'tv', 'tv', '', ''); ?>
<br>
<br />
Special Requirements:<br />
    
<?php input_textarea('specialreq', ''); ?>
<br />
<?php input_submit('request', 'Make Request'); ?>
<input type = 'hidden' name='_submit_check' value = '1' />
</form>
</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p></td></tr>
</table>
<?php
}
?>
<?php
function validate_form(){
$errors = array();
//no of students
if (empty($_POST['students'])){
$errors[] = 'Please enter number of students';
}
else if ($_POST['students'] != strval(intval($_POST['students']))){
$errors[] = 'Please enter numbers only in number of students field';
}
else if (($_POST['students'] <1)){
$errors[] = 'Please enter a number of students greater than 0';
}
else if (($_POST['students'] >300)){
$errors[] = 'There are no rooms available for that number of students. The maximum capacity is 300.';}
return $errors;
}
function process_form(){
/*$connection = mysql_connect($db_host, $db_username, $db_password);
if (! $connection){
die ("Could not not connect to database: " .mysql_error());
}
$db_select = mysql_select_db($db_database);
if (! $db_select){
die ("Could not select database: " .mysql_error());
}*/
$getpriority = $_POST['priority'];
$getmodules = $_POST['modules'];
$getrooms = $_POST['roomcode'];
$getarea = $_POST['area'];
$getstudents = $_POST['students'];
$getday = $_POST['day'];
// assigns the 'week' array to getweeks. Not sure if it works
$getweeks = $_POST[weeks];
$getstart = $_POST['start'];
$getend = $_POST['end'];
$getfacilities = array($_POST['blackboard'], $_POST['whiteboard'], $_POST['computer_point'],$_POST['network'],$_POST['projector'],$_POST['microphone'], $_POST['tv']);
$getspecialreq = $_POST['specialreq'];
echo "<br> $getpriority";
echo "<br> $getmodules";
echo "<br> $getrooms";
echo "<br> $getarea";
echo "<br> $getstudents";
echo "<br> $getday";
//I'm trying to lop through the 'weeks' array and print out which checkboxes have been ticked
foreach($_POST[weeks] as $reqweeks)
{
echo $reqweeks;
}
echo "<br> $getstart";
echo "<br> $getend";
echo "<br> $getspecialreq";
//mysql_close($connection);
}
?>
</body>
</html>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]