Rather than repeating all that code, I suggest the following:


<select name="type" id="type>
   <option value="0">-- select type --</option>
<option value="meeting" <?php echo (isset($_POST['hidSubmit'] && $_POST['hidSubmit'] == TRUE && $_POST['type'] == "meeting") ? "selected" : '' ?> <option value="event" <?php echo (isset($_POST['hidSubmit'] && $_POST['hidSubmit'] == TRUE && $_POST['type'] == "event") ? "selected" : '' ?>
</select>


For a month selector, try:

<?php
   // assume current month number is in $curr_month
$months = array(1 => "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); // will create an array of month names, with a starting base of 1 (instead of zero)

  echo "<select name=\"month\" id=\"month\">\n";
  foreach ($months as $m => $mname) {
    echo "<option value=\"$m\"";
    if ($curr_month == $m) echo " selected ";
    echo ">$mname</option>\n";
  }
  echo "</select>\n";
?>

There are other possiblities as well. One time, I didn't want to actually store the month names, perhaps allowing them to be localized. Instead I used strftime, which will return appropriate names based on locale:

<?php
  // assume current month number is in $curr_month

  echo "<select name=\"month\" id=\"month\">\n";
  for ($m = 1; $m <= 12; $m++) {
    echo "<option value=\"$m\"";
    if ($curr_month == $m) echo " selected ";
    echo ">";
$mname = strftime("%B", 0,0,0,2010, $m, 1); // time, year and day don't matter, all we're after is the appropriate month name set in the locale
    echo $mname;
    echo "</option>\n";
  }
  echo "</select>\n";
?>




On Sep 11, 2010, at 8:49 AM, Jason Pruim wrote:

Hey everyone!

Hope you are having a great weekend, and I'm hoping someone might be coherent enough to help me find a more elegant solution to a problem that I have...

I have a form for submitting an event to a website, and if the form is not submitted successfully (such as they didn't fill out a required field) I want it to redisplay the form with inline errors as to what happened and display the values they selected...

I have a working solution but was hoping for something a little more elegant. And something that would work better for a month selector as well... Here is the relevant code that I have that works:

<?PHP
       if ($_POST['hidSubmit'] ==TRUE & $_POST['type'] == "meeting"):
echo <<<HTML
       <select name="type" id="type">
           <option value="0">-- select type --</option>
           <option value="meeting" selected>Meeting</option>
           <option value="event" >Event</option>
       </select>
HTML;

elseif ($_POST['hidSubmit'] == TRUE & $_POST['type'] == "event"):
//if ($_POST['hidSubmit'] == TRUE & $_POST['type'] == "event") {

echo <<<HTML
       <select name="type" id="type">
           <option value="0">-- select type --</option>
           <option value="meeting">Meeting</option>
           <option value="event" selected>Event</option>
       </select>
HTML;

else:
//if ($_POST['hidSubmit'] != TRUE):


echo <<<HTML
       <select name="type" id="type">
           <option value="0" selected>-- select type --</option>
           <option value="meeting">Meeting</option>
           <option value="event">Event</option>
       </select>
HTML;
endif;

?>

which works BUT I don't want to have to have that for a month selector or a day selector :)

Any ideas what I'm missing?



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to