index.html
Code: Select all
<hmtl>
<body>
<center><h2>Welcome<h2></center>
<p>
<b>Enroll a student:</b>
<form name="enroll" action="script.php" method="post">
First Name:<input type="text" name="firstname" size="8">
<br>
Class:
<li><input type="radio" name="class[]" value="classone">classone<br>
<li><input type="radio" name="class[]" value="classtwo">classtwo<br>
<li><input type="radio" name="class[]" value="classthree">classthree<br>
<input type="submit" name="enrollsubmit" value="Enroll!">
</form
</p>
<p>
<br>
<br>
<b>Search for a student:</b>
<form name="search" action="script.php" method="post">
Name:<input type="text" name="searchname" size ="13">
<br>
<input type="submit" name="searchsubmit" value="Search!">
</form>
</p>
</body>
</html>
Code: Select all
<?php
//Define arrays - These are the 'Classes'
$classone = array(bob, john);
$classtwo = array(jane, mary);
$classthree = array(james, tony);
$temparray = array();
//Define Variables
$name = $_POST['firstname'];
$class = $_POST['class'];
$searchname = $_POST['searchname'];
//If script is loaded without clicking through a button:
if ((!isset($_POST['searchsubmit'])) && (!isset($_POST['enrollsubmit']))) {
echo "Error - Form Not Submitted";
}
//This is the Search function
/*
Plan here is to take string from form, search through classrooms for that string, then return the classroom of that student. If no match is found,error.
*/
//**************************************************************************
//**************************************************************************
elseif ((isset($_POST['searchsubmit'])) && (!isset($_POST['enrollsubmit']))){
echo "<h2>Search Submitted</h2><br>";
echo "You searched for <b>$searchname</b>.<br>";
$found = false;
if ($found == false) {
foreach ($classone as $a) {
if ($a == $searchname) {
echo "found $searchname in Class One";
$found = true;
}
}
}
elseif ($found == false) {
foreach ($classtwo as $a) {
if ($a == $searchname) {
echo "found $searchname in Class Two";
$found = true;
}
}
}
elseif ($found == false) {
foreach ($classthree as $a) {
if ($a == $searchname) {
echo "found $searchname in Class Three";
$found = true;
}
}
}
elseif ($found == false) {
echo "Sorry, <b>$searchname</b> not found";
}
}
//**************************************************************************
//**************************************************************************
//This is the Enroll Function
elseif ((isset($_POST['enrollsubmit'])) && (!isset($_POST['searchsubmit']))){
echo "Enrollment Submitted";
echo "<br>";
echo $name;
echo "<br>";
echo $class;
echo "<br>";
if (($name == "") && ($class =="")) {
echo "Sorry, No data enterd";
}
elseif (($name =="") && ($class !="")){
echo "No name enterd!!";
}
elseif (($name !="") && ($class =="")){
echo "No Class enterd!!";
}
//All required data is present for enrollment
/*
Would like to add function to perform check of class size before adding another student. If classsize >10 then error.
Added functionality could be to then search for a space in a classroom.
*/
elseif (($name !="") && ($class !="")){
if (is_array($_POST['class'])) {
//define element from posted array as $a, then add $a to temparray
foreach ($_POST['class'] as $a);
array_push($temparray, $a);
}
//Add Submitted name to temparray
array_push($temparray, $name);
//This script adds $name to the correct class dependant on the information in the [0] position of $temparray
switch ($temparray[0]) {
case 'classone':
echo "$name has been enrolled into Class One";
array_push($classone, $temparray[1]);
break;
case 'classtwo':
echo "$name has been enrolled into Class Two";
array_push($classtwo, $temparray[1]);
break;
case 'classthree':
echo "$name has been enrolled into Class Three";
array_push($classthree, $temparray[1]);
break;
}
}
}
else {
echo "Unknown Error!";
}
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
print_r($classone);
echo "<br>";
print_r($classtwo);
echo "<br>";
print_r($classthree);
?>
OK, i am taking a string from my form 'searchsubmit' and then going through the arrays to find the string to see if it exists. I am using the $found variable to stop searching if the name has been found already. The search works if the name is in 'classone' but not in the next two 'classes'. the script seems to be exiting after seraching the first array and im not sure why. Anyone got any ideas?