please help with printing values of check boxes.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ant_sutton
Forum Commoner
Posts: 32
Joined: Thu May 05, 2005 5:27 am

please help with printing values of check boxes.

Post by ant_sutton »

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:&nbsp&nbsp&nbsp&nbsp<?php  input_select('priority','normal', $priorities); ?> <br />

Module Code:&nbsp&nbsp&nbsp&nbsp<?php input_select('modules', '', $modules); ?> <br />

Room Code:&nbsp&nbsp&nbsp&nbsp<?php input_select('roomcode', '', $rooms); ?> <br />

Area:&nbsp&nbsp&nbsp&nbsp<?php input_select('area', '', $area); ?> <br />

No. of Students:&nbsp&nbsp&nbsp&nbsp <?php input_text('students', '',3); ?><br />

Day:&nbsp&nbsp&nbsp&nbsp <?php input_select('day','',$day); ?><br />



Weeks:<br />&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'Full Semester', '', 'checked'); ?> <br>
&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week1', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week2', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week3', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week4', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week5', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week6', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week7', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week8', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week9', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week10', '', ''); ?> <br>

&nbsp&nbsp&nbsp&nbsp<?php input_radiocheck('checkbox', 'weeks[]', 'week11', '', ''); ?> <br>



Start Time:&nbsp&nbsp&nbsp&nbsp<?php input_select('start', '', $times); ?> <br /> 

End Time:&nbsp&nbsp&nbsp&nbsp<?php input_select('end', $times[1], $times); ?> </td>

  <td><p>Facilities:<br />
&nbsp&nbsp&nbsp&nbsp
<?php input_radiocheck('checkbox', 'blackboard', 'blackboard', '', ''); ?> 
  <br>
&nbsp&nbsp&nbsp&nbsp
<?php input_radiocheck('checkbox', 'whiteboard', 'whiteboard', '', ''); ?> 
  <br>
&nbsp&nbsp&nbsp&nbsp
<?php input_radiocheck('checkbox', 'computer point', 'computer_point', '', ''); ?> 
  <br>
&nbsp&nbsp&nbsp&nbsp
<?php input_radiocheck('checkbox', 'network', 'network', '', ''); ?> 
  <br>
&nbsp&nbsp&nbsp&nbsp
<?php input_radiocheck('checkbox', 'projector', 'projector', '', ''); ?> 
  <br>
&nbsp&nbsp&nbsp&nbsp
<?php input_radiocheck('checkbox', 'microphone', 'microphone', '', ''); ?> 
  <br>
&nbsp&nbsp&nbsp&nbsp
<?php input_radiocheck('checkbox', 'tv', 'tv', '', ''); ?> 
  <br>
  <br />
      
      
      
      
      
      
      Special Requirements:<br />
&nbsp&nbsp&nbsp&nbsp
<?php input_textarea('specialreq', ''); ?> 
      <br />
        <?php input_submit('request', 'Make Request'); ?>
		
		<input type = 'hidden' name='_submit_check' value = '1' />
      
        </form>
		

      
    </p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;  </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]
ant_sutton
Forum Commoner
Posts: 32
Joined: Thu May 05, 2005 5:27 am

re

Post by ant_sutton »

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]


O and here is the code for the formhelprs.php that I require at the start of the script.  thanks

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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

//print a text box

function input_text($element_name, $values, $size) {

    echo"<input type='text' name='" . $element_name ."' size='".$size."' value='";

    echo htmlentities($values[$element_name]) . "'>";

}



//print a submit button

function input_submit($element_name, $label) {

    print '<input type="submit" name="' . $element_name .'" value="';

    print htmlentities($label) .'"/>';

}



//print a textarea

function input_textarea($element_name, $values) {

    print '<textarea name="' . $element_name .'">';

    print htmlentities($values[$element_name]) . '</textarea>';

}



//print a radio button or checkbox

function input_radiocheck($type, $element_name, $label, $element_value, $checked) {

    echo "$label<input type='" . $type . "' name='" . $element_name ."' value='" . $element_value . " ' $checked";

    

    print '/>' ;

}



//print a <select> menu

function input_select($element_name, $selected, $options, $multiple = false) {

    // print out the <select> tag

    print '<select name="' . $element_name;

    // if multiple choices are permitted, add the multiple attribute

    // and add a [  ] to the end of the tag name

    if ($multiple) { print '[  ]" multiple="multiple'; }

    print '">';



    // set up the list of things to be selected

    $selected_options = array( );

    if ($multiple) {

        foreach ($selected[$element_name] as $val) {

            $selected_options[$val] = true;

        }

    } else {

        $selected_options[ $selected[$element_name] ] = true;

    }



    // print out the <option> tags

    foreach ($options as $option => $label) {

        print '<option value="' . htmlentities($option) . '"';

        if ($selected_options[$option]) {

            print ' selected="selected"';

        }

        print '>' . htmlentities($label) . '</option>';

    }

    print '</select>';

}

?>
</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]
ant_sutton
Forum Commoner
Posts: 32
Joined: Thu May 05, 2005 5:27 am

re

Post by ant_sutton »

Hi guys.

Don't worry, problem solved :)
Post Reply