1

以下にドロップダウンメニューがあります。

  $form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td><select name='year' id='yearDrop'>
<option value=''></option>
<option value='$getyear[1]'>1</option>
<option value='$getyear[2]'>2</option>
<option value='$getyear[3]'>3</option>
<option value='$getyear[4]'>4</option>
<option value='$getyear[5]'>5</option>
<option value='$getyear[6]'>6</option>
<option value='$getyear[7]'>7</option>
<option value='$getyear[8]'>8</option>
<option value='$getyear[9]'>9</option>
<option value='$getyear[10]'>10</option>
</select></td>
</tr>
</table>
</form>
";

ドロップダウンメニューがフォームのphp変数にあることに注意してください。私の質問は、「上のドロップダウンメニューを表示するためのはるかに優れた短い方法はありますか?」です。たぶん、ある種のループを使用して各値をループし、各オプションに同じvalue属性を与えることによってですか?

getyear単純なifステートメントに合わせるために使用したいと思います。

    if ($getyear){
    echo "year is chosen";
    }else{
    $errormsg = "You must enter in Student's current Academic Year to Register";
              }

アップデート:

<?php  

  $getyear = (isset($_POST['year'])) ? $_POST['year'] : '';
  $errormsg = (isset($errormsg)) ? $errormsg : '';

$min_year = 1;
$max_year = 10;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
$yearHTML = '';
$yearHTML .= '<select name="year" id="yearDrop">'.PHP_EOL; 
$yearHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
foreach ($years as $year) {
    $yearHTML .= "<option>$year</option>".PHP_EOL;  // if no value attribute, value will be whatever is inside option tag, in this case, $year
}
$yearHTML .= '</select>'; 

  if( (isset($_POST['registerbtn']))){
      $getyear = $_POST['year'];

      if (!in_array($getyear , $years)){
      echo "year is chosen";
                    }else{
              $errormsg = "You must enter in Student's current Academic Year to Register";
          }

            }

  $form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td></td>
  <td id='errormsg'>$errormsg</td>
  </tr>
  <tr>
  <td>Year:</td>
  <td>{$yearHTML}</td>
  <td><input type='text' name='year' value='$getyear' /></td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Register' name='registerbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;

?>
4

2 に答える 2

1

ループは、foreach配列をループしてその値に対して操作を実行するときに非常に役立ちます。

foreach制御構造

<?php
// reference values for output and for validation later
$min_year = 1900;  // using as an example
$max_year = 2012;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012

// for HTML output
if (empty($_POST)) {  // display form
    $html = '';
    foreach ($years as $year) {
        $html .= "<option>$year</option>";  // if no value attribute, value will be whatever is inside option tag, in this case, $year
    }
    // output HTML
    echo $html;
} else {  // process form
    $chosen_year = isset($_POST['year']) ? $_POST['year'] : "";
    if (!in_array($chosen_year , $years) {  // year is invalid
        $errormsg = "You must enter in Student's current Academic Year to Register";
    } else {  // year is chosen
        echo 'year is chosen';
    }
}
于 2012-11-22T02:03:33.630 に答える
0

このファイルはselectオプションを自動的に生成し、さらに処理を行うための簡単な場所を提供します。

function process_form(){
    if(!isset($_POST['year']) || !is_numeric($_POST['year'])){
        // Form invalid
        return false;
    }

    $year   = $_POST['year'];

    // Perform actions - save to database, etc..?

    // Form valid
    return true;
}

$error  = false;
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // Form submitted
    $result = process_form();
    if($result){
        ?>
        <p>Valid year selected</p>
        <?php
        exit;
    }

    $error  = true;
}
?>
<form action="" method="post">
<?php if($error){ ?>
    <p class="error">You must select the student's current academic year to register</p>
<?php } ?>
<label for="select_year">Academic Year:</label>
<select name="year" id="select_year">
    <option value=""></option>
    <?php for($year = $start_year; $year <= $end_year; $year++){ ?>
        <option value="<?php echo $year;?>"><?php echo $year;?></option>
    <?php } ?>
</select>

<button type="submit">Register</button>
</form>
于 2012-11-22T02:06:07.967 に答える