Jump to content

Form Help [solved]


Recommended Posts

Im creating a website for a uni project and need some help with forms. The website i am talking about can be found at www.housedarus.co.uk/ECE/admin/coursetest.php. The login is admin password =password.

 

Heres the problem I need some kind of field to let users choose units that will belong to a course therefore i need a way for a user to select upto 36 units which will then be placed in a database.

 

The question i put before you all is how should i let the users select these units. If yoyu look at the page above you can see i have tried using option field and increased its size and set it as multiple however although i choose multiple units only one unit is returned in the post field for the option box.

 

Another way i could do it is to set lots off option fields however this is a bit cumbersome and not really open enough to allow you to select as many units as you want.

 

Have a look at the site if this post is not very clear. basically how do i get option to return multiple options.

 

This is the code for the page.

<?php
session_start();
//This script adds a user to the users table

//Check if there is a current session if not send user to login page
if ($_SESSION[valid] != "yes")
   {
       header("location: http://www.housedarus.co.uk/ECE/adminlogin.php");
       exit;
   }

$validUser = $_SESSION['user'];

include '../includes/sql_connect.php';

//select course to place in option box in form
$units = "SELECT  unitName unitCode FROM units ";

$result = @mysql_query($units,$connection) or die(mysql_error());

//create options for the forms course options
while ($row = mysql_fetch_array($result))
   {
       $unitName = $row['unitName'];
       $unitCode = $row['unitCode'];
       
        $select_block .= "<option value =\"$unitCode\">$unitName $unitCode</option>";
   }

include "../includes/header.php";

//Display navigation links
echo "<P><CENTER>
<A HREF=\"http://www.housedarus.co.uk/ECE/admin/administrators.php\" BORDER=\"0\"><IMG SRC=\"http://www.housedarus.co.uk/ECE/images/buttonsAdmin.png\" BORDER=\"0\"></A>
<A HREF=\"http://www.housedarus.co.uk/ECE/admin/users.php\" BORDER=\"0\"><IMG SRC=\"http://www.housedarus.co.uk/ECE/images/buttonsUsers.png\" BORDER=\"0\"></A>
<A HREF=\"http://www.housedarus.co.uk/ECE/admin/lecturers.php\" BORDER=\"0\"><IMG SRC=\"http://www.housedarus.co.uk/ECE/images/buttonsLecturers.png\" BORDER=\"0\"></A>
<A HREF=\"http://www.housedarus.co.uk/ECE/admin/courses.php\" BORDER=\"0\"><IMG SRC=\"http://www.housedarus.co.uk/ECE/images/buttonsCourses.png\" BORDER=\"0\"></A>
<A HREF=\"http://www.housedarus.co.uk/ECE/admin/units.php\" BORDER=\"0\"><IMG SRC=\"http://www.housedarus.co.uk/ECE/images/buttonsUnits.png\" BORDER=\"0\"></A>
<A HREF=\"http://www.housedarus.co.uk/ECE/admin/cabinets.php\" BORDER=\"0\"><IMG SRC=\"http://www.housedarus.co.uk/ECE/images/buttonsCabinets.png\" BORDER=\"0\"></A></CENTER></P>";

//if a form has been submitted
if ($_POST[submission] == "true")
   {
       //Check if details have been entered if not display message
       if (!$_POST[courseCode] || !$_POST[courseName])
       {
            echo "You have not entered all the details required please try again";
       }
       //insert into table if details are entered
       else
       {
           $courseCode = $_POST[courseCode];
           $courseName = $_POST[courseName];
           $query = "INSERT into courses values ('$courseCode', '$courseName')";
           $result = @mysql_query($query,$connection) or die(mysql_error());
           $count = "1";
           
           
           echo "<TABLE ALIGN=\"CENTER\"><TR><TD BGCOLOR=\"E2D3EF\">";
           echo "course $_POST[courseName] inserted into database courses with the following data:<BR>";
           echo "courseCode = $_POST[courseCode]<br>";
           echo "courseName $_POST[courseName]<BR>";
           echo "Units Entered $_POST[units]";
           
           echo "</TR></TD></TABLE>";
                 
       }
   }
   
//if delete button pressed delete the user
if ($_POST[deletehidden] == "true")
   {
       $delHemis = $_POST[delCourse];
       $delete = "DELETE from courses WHERE courseCode='$delCourse'";
       $result = @mysql_query($delete,$connection) or die (mysql_error());
       
       echo "<TABLE ALIGN=\"CENTER\"><TR><TD BGCOLOR=\"E2D3EF\">";
       echo "User $_POST[delCourse] deleted from database";
        echo "</TR></TD></TABLE>";
       
   }

//if modify button pressed display modification form or run query
if ($_POST[modifyTrue] == "true")
   {
       if ($_POST[commitmodify] == "true")
       {
           if ($_POST[courseName])
           {
               $updateCourseName = "UPDATE courses SET courseName = '$_POST[courseName]' WHERE courseCode = '$_POST[courseCodeOld]'";
               $courseNameResult = mysql_query($updateCourseName,$connection) or die (mysql_error());
           }
           if ($_POST[courseCodeNew])
           {
               $updateCourseCode = "UPDATE courses SET courseCode = '$_POST[courseCodeNew]' WHERE courseCode = '$_POST[courseCodeOld]'";
               $courseCodeResult = mysql_query($updateCourseCode,$connection) or die (mysql_error());
           }
           if ($courseNameResult || $courseCodeResult)
           {
               echo "<TABLE ALIGN=\"CENTER\"><TR><TD BGCOLOR=\"E2D3EF\">";
               echo "The following details have been changed:<P><UL SHAPE=\"disc\"";
               if ($courseNameResult)
               {
                   echo "<LI>The new courseName is $_POST[courseName]";
               }
               if ($courseCodeResult)
               {
                   echo "<LI>The new courseCode is $_POST[courseCodeNew]";
               }
               echo "</P></UL>";
                echo "</TR></TD></TABLE>";
           }

           else
           {
             echo "<TABLE ALIGN=\"CENTER\"><TR><TD BGCOLOR=\"E2D3EF\">";
               echo "You have not entered any new details";
                echo "</TR></TD></TABLE>";
           }
       }
       else
       {
           echo "<CENTER><P><TABLE>
           <TR BGCOLOR=\"9E84BE\"><TD>Course Code &nbsp</TD><TD>Course Name &nbsp</TD></TR>
           <TR BGCOLOR=\"E2D3EF\"><TD>$_POST[courseCode]</TD><TD>$_POST[courseName]</TD></TR></TABLE><P></CENTER>";

           echo "<CENTER><form method=post action=\"$_SERVER[PHP_SELF]\"><FIELDSET><LEGEND> MODIFY COURSE</LEGEND>
           <input type=\"hidden\" name=\"modifyTrue\" value=\"true\">
           <input type=\"hidden\" name=\"commitmodify\" value=\"true\">
           <input type=\"hidden\" name=\"courseCodeOld\" value=\"$_POST[courseCode]\">

           &nbsp<strong>Course Code:</strong>
           <input type=\"text\" name=\"courseCodeNew\" size=15 maxlength=6>&nbsp

 	&nbsp<strong>Course Name:</strong>
 	<input type=\"text\" name=\"courseName\" size=15 maxlength=25>&nbsp
           <BR>
 	<input type=\"submit\" name=\"submit\" value=\"Modify Course\"></CENTER></FIELDSET>
           </FORM>";
       }

   }


//if form has not been submitted show form to enter details
       $courses = "SELECT  courseCode, courseName FROM courses ";

       $coursesresult = @mysql_query($courses,$connection) or die(mysql_error());

       $lineRow = "even";

       //while a row exists add to table
       while ($row = mysql_fetch_array($coursesresult))
       {
           $courseCode = $row['courseCode'];
           $courseName = $row['courseName'];
                       //Create table rows
           if ($lineRow == "even")
           {
               $table_block .= "<TR BGCOLOR=\"E2D3EF\"><TD>$courseCode</TD><TD>$courseName</TD><TD>
               <form method=post action=\"$_SERVER[PHP_SELF]\">
               <input type=\"hidden\" name=\"delCourse\" value=\"$courseCode\">
               <input type=\"hidden\" name=\"deletehidden\" value=\"true\">
               <input type=\"submit\" name=\"delete\" value=\"Delete\">
               </form></TD>
               <TD><form method=post action=\"$_SERVER[PHP_SELF]\">
               <input type=\"hidden\" name=\"modifyTrue\" value=\"true\">
               <input type=\"hidden\" name=\"courseCode\" value=\"$courseCode\">
               <input type=\"hidden\" name=\"courseName\" value=\"$courseName\">
               <input type=\"submit\" name=\"modify\" value=\"Modify\">
               </form></TD></TR>";
               $lineRow = "odd";
           }
             else
           {
             $table_block .= "<TR BGCOLOR=\"9E84BE\"><TD>$courseCode</TD><TD>$courseName</TD><TD>
               <form method=post action=\"$_SERVER[PHP_SELF]\">
               <input type=\"hidden\" name=\"delCourse\" value=\"$courseCode\">
               <input type=\"hidden\" name=\"deletehidden\" value=\"true\">
               <input type=\"submit\" name=\"delete\" value=\"Delete\">
               </form></TD>
               <TD><form method=post action=\"$_SERVER[PHP_SELF]\">
               <input type=\"hidden\" name=\"modifyTrue\" value=\"true\">
               <input type=\"hidden\" name=\"courseCode\" value=\"$courseCode\">
               <input type=\"hidden\" name=\"courseName\" value=\"$courseName\">
               <input type=\"submit\" name=\"modify\" value=\"Modify\">
               </form></TD></TR>";
               $lineRow = "even";
           }
       }
       if ( !$_POST[modifyTrue]  || ($_POST[modifyTrue] == "true" && $_POST[commitmodify] == "true" ))
       {
       echo "<CENTER><P>
           <form method=post action=\"$_SERVER[PHP_SELF]\"><FIELDSET><LEGEND>Add Course Information</LEGEND>
 	$msg
      &nbsp<strong>Course Code:</strong>
 	<input type=\"text\" name=\"courseCode\" size=5 maxlength=6>&nbsp

 	&nbsp<strong>Course Name:</strong>
 	<input type=\"text\" name=\"courseName\" size=15 maxlength=25>&nbsp<BR>
           Please Enter the units available on this Course:<BR>";
 	
           echo "<TABLE><TR><TD>";
 
                    echo "
           <select name=\"units\" SIZE=10 MULTIPLE>
           <OPTION SELECTED VALUE=\"\">
                    $select_block
                    </select>";
            
             echo "<TABLE><TR><TD>";


           echo"</td></td></TABLE><input type=\"hidden\" name=\"submission\" value=\"true\"><BR>
           
 	<input type=\"submit\" name=\"submit\" value=\"Submit New User\"></CENTER>
 	</form><P>";
 	}
 	
 	echo "<TABLE align=\"center\"><TR BGCOLOR=\"9E84BE\"> <TD WIDTH =100>Course Code</TD><TD>CourseName</TD><TD></TD><TD></TD></TR>";
           echo "$table_block";
           echo "</TABLE>";


include "../includes/footer.php";

?>

 

The Proplem is solved now i needed to put the options into an array.

 

[moved from Talk-Talk by spinynorman]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...