0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHPでselectBox作ってみた

Last updated at Posted at 2021-09-14

PHPでselectBox作ってみた

タイトル通りです。

<?php

$busho_list = array("営業部","経理部","製造部","開発部",);
$select_option = 2;

$select_box = "<select name='busho'>";
foreach($busho_list as $key => $busho){

    $selected = "";
    if($key === $select_option){
        $selected = "selected";
    }
    $select_box .= "<option value='{$key}' {$selected}>{$busho}</option>";
}
$select_box .= "</select>";

echo $select_box;

変数$select_optionにデータベースから取得した値を代入することでデフォルトの選択を動的に変更することができます。

$busho_list自体もマスタなどから取得すれば選択項目も動的に作ることができますね♪

部署みたいに会社によって大きく異なる要素はPHPで動的に作ってあげることで、どんなに部署が増えてもHTMLを修正することなく選択肢を追加することができました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?