Page 1 of 1

How to redisplay selected value in a list menu on error

Posted: Sun Dec 06, 2009 11:24 am
by franzlin2010
good day all,

Please i have a form with list menus where students select their course and grades. and then submit them.

may be if an error occours how do i regisplay the the option the user selected in the list menu.
I was able to do this with a static html form, but dont know how to go about it in a dynamic generated form, here is my code that generated the form.

Code: Select all

 
<?php
// this is the array for the subject select menu items.//
 $ssce = array("Subject" => "",
  "Elective Mathematics" => "mathematics",
  "Core Mathematics" => "core-maths", 
  "English" => "english",     
  "Physics" => "physics", 
  "Chemistry" => "chemistry",     
  "Biology" => "biology",     
  "Geography" => "geo", 
  "Intergrated Science" => "int-sci", 
  "Social Studies"=> "social",);
// here is the grade for the grade select item menu//
$grades = array("A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9",);
  
// here is the function that i call to construct the form
function subjects($ssce)
 {
    echo "<li><select name=\"subject[]\">\n";
      foreach($ssce as $option => $value)
      {
        echo "<option value=\"".$value."\">\n".$option."</option>\n";
      }
   echo "</select>\n";
} 
 
// here is the function for the grade select menu items//
function grades1($grades)
 {
    echo "<select name=\"grade[]\">\n";
   // echo'<option value="">grade</option>';
      foreach($grades as $grade)
      {
        echo "<option value=\"".$grade."\">\n".$grade."</option>\n";
      }
        echo "</select>\n</li>";
 } 
 
/// here is the last part of the code where i output the form//
echo'<form name="subjects" method="POST" action="'.$_SERVER['PHP_SELF'].'"><ol>';
for($a=1; $a<=5; $a++)
{
    subjects($ssce); grades1($grades);
}
echo'</ol>';
echo'<input type="submit" name="submit" value="Register" />';
?>
 
Please how do i redisplay the users selected option in case the user selected more that one subject at a time can anybody give me a clue?

Thanks alot in advance

Re: How to redisplay selected value in a list menu on error

Posted: Sun Dec 06, 2009 5:00 pm
by BlaineSch

Code: Select all

 
<?php
// this is the array for the subject select menu items.//
$ssce = array("Subject" => "",
                "Elective Mathematics" => "mathematics",
                "Core Mathematics" => "core-maths", 
                "English" => "english",     
                "Physics" => "physics", 
                "Chemistry" => "chemistry",     
                "Biology" => "biology",     
                "Geography" => "geo", 
                "Intergrated Science" => "int-sci", 
                "Social Studies"=> "social",);
$grades = array("A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9",);
function subjects($ssce, $a, $post) {
    echo "<li><select name=\"subject[{$a}]\">\n";
    foreach($ssce as $option => $value) {
        $selected = ($post==$value)?' selected':'';
        echo "<option value=\"".$value."\"{$selected}>\n".$option."</option>\n";
    }
    echo "</select>\n";
} 
 
// here is the function for the grade select menu items//
function grades1($grades, $a, $post) {
    echo "<select name=\"grade[{$a}]\">\n";
    // echo'<option value="">grade</option>';
    foreach($grades as $grade) {
        $selected = ($post==$grade)?' selected':'';
        echo "<option value=\"".$grade."\"{$selected}>\n".$grade."</option>\n";
    }
    echo "</select>\n</li>";
} 
 
/// here is the last part of the code where i output the form//
echo'<form name="subjects" method="POST" action="'.$_SERVER['PHP_SELF'].'"><ol>';
for($a=1; $a<=5; $a++) {
    subjects($ssce, $a, $_POST['subject'][$a]);
    grades1($grades, $a, $_POST['grade'][$a]);
}
echo'</ol>';
echo'<input type="submit" name="submit" value="Register" />';
?>
 
Try that on for size, this is untested but I'm sure you can see how it works.

Re: How to redisplay selected value in a list menu on error

Posted: Mon Dec 07, 2009 4:09 am
by franzlin2010
Please can you tell me more about the for size?
cos this is somehow urgent.

thnx in advance