Jump to content

PHP Variables and POST


Nitestarz78

Recommended Posts

I have a form that autofills in the course name depending on what the user chose. When I reload the page this selection is gone. The page is reloaded with an error message if the user didnt fill out all the necessary fields. How can I get this drop down menu to save it's selection when the page is reloaded?

 

<?
$course_array = array(
'1',
'2',
'3',
'4',
'5'
);
print "<select name=Course>";
  $i = 0;
  WHILE ($i < count($course_array))
  {
  if($course_array[$i]==$CourseName)
      print "<option selected "; 

  else 
print "<option";
print " value='$course_array[$i]'> $course_array[$i]";
print "</option>";
    $i++;
   }
   print "</select>"; 
   ?>

 

Please help!

Link to comment
https://forums.phpfreaks.com/topic/140147-php-variables-and-post/
Share on other sites

try this

 

<?php
$course_array = array(
'1',
'2',
'3',
'4',
'5'
);
print "<select name=Course>";
$i = 0;
if(isset($_POST['Course') && $_POST['Course'] == $course_array[$i]) $CourseName = "selected=\"selected\"";
else $CourseName = "";
WHILE ($i < count($course_array))
{
if($course_array[$i]==$CourseName)
	print "<option selected ";
else{
	print "<option";
	print " value='$course_array[$i]'> $course_array[$i]";
	print "</option>";
	$i++;
}
}
print "</select>";
?>

EDIT

 

<?php
$course_array = array(
   '1',
   '2',
   '3',
   '4',
   '5'
   );
print "<select name=Course>";
$i = 0;
if(isset($_POST['Course']) && $_POST['Course'] == $course_array[$i]) $CourseName = "selected=\"selected\"";
else $CourseName = "";
WHILE ($i < count($course_array))
{
   if($course_array[$i]==$CourseName)
      print "<option selected ";
   else{
      print "<option";
      print " value='$course_array[$i]'> $course_array[$i]";
      print "</option>";
      $i++;
   }
}
print "</select>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.