HTML_QuickForm::addElement()

■code
 
object  &HTML_QuickForm::addElement(mixed $element)
 
■説明 フォームに要素を追加します。
要素の種類により、パラメータが異なります。
 
■パラメータ説明 ※主な要素のみ記述しています。
element
(arg1)
arg2 arg3 arg4 arg5 arg6
"button" string
element-name
button名
string value
buttonに付ける
文字
mixed attributes
その他の属性
   
"checkbox" string
element-name
checkbox名
string
element-label
checkboxの前に
付くラベル
string text
checkboxの後に
付くラベル
mixed attributes
その他の属性
 
"header" string
element-name
header名
string text
headerの内容
     
"hidden" string
element-name
hidden要素名
string value
hiddenの値
mixed attributes
その他の属性
   
"link" string
element-name
link名
string label
link項目のラベル
string href
linkのURI
string text
表示text
mixed attributes
その他の属性
"password" string
element-name
password要素名
string label
passwordのラベル
mixed attributes
その他の属性
   
"reset" string
element-name
要素名
string value
ボタンのラベル
mixed attributes
その他の属性
   
"select" string
element-name
select要素名
string label
selectのラベル
mixed data
selectデータ
mixed attributes
その他の属性
 
"submit" string
element-name
submit要素名
string value
submitのラベル
mixed attributes
その他の属性
   
"text" string
element-name
text要素名
string label
textのラベル
mixed attributes
その他の属性
   
"textarea" string
element-name
textarea要素名
string label
textareaのラベル
mixed attributes
その他の属性
   

※「checkbox」「submit」については、HTML_QuickForm::createElement()と
 HTML_QuickForm::addGroup()も参照。
 
■example
<?php
// button
    $_form->addElement('button', 'okbutton', 'OK'); 

// checkbox
    $_form->addElement('checkbox', 'answer', 'yes', 'はい'); 

// header
    $_form->addElement('header', 'myHeader', 'ヘッダーテスト'); 

// hidden
    $_form->addElement('hidden', 'customer_no', '12345');

// link
    $_form->addElement('link', null, 'こちらを参照', 
                       '../01_HTML_QuickForm/', 'HTML_QuickForm');

// password
    $_form->addElement('password', 'passwd', 'パスワード',
                       array('size' => 20, 'maxlength' => 20));

// reset
    $_form->addElement('reset', 'CLEAR', 'クリア');

// select
    $colorArr = array("0"=>"赤", 
                      "1"=>"オレンジ", 
                      "2"=>"黄", 
                      "3"=>"緑", 
                      "4"=>"青", 
                      "5"=>"紫", 
                      "6"=>"水色", 
                      "7"=>"白"); 
    $_form->addElement('select', 'selColor', '好きな色:', $colorArr); 

// submit
    $_form->addElement('submit', 'SUBMIT', '送信'); 

// text
    $_form->addElement('text', 'name', '名前',
                       array('size' => 25, 'maxlength' => 25));

// textarea
    $_form->addElement('textarea', 'txaInp', '感想:');

?>
■生成される
html
<form action="sample01.php" method="post" name="form01" id="form01">
<div>
<input name="customer_no" type="hidden" value="12345" />
<table border="0">
  <tr>
    <td align="right" valign="top"><b></b></td>
    <td valign="top" align="left">
    <input name="okbutton" value="OK" type="button" /></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>yes</b></td>
    <td valign="top" align="left">
    <input name="answer" type="checkbox" value="1" id="qf_163c8f" />
    <label for="qf_163c8f">はい</label></td>
  </tr>
  <tr>
    <td style="white-space: nowrap; background-color: #CCCCCC;"
     align="left" valign="top" colspan="2"><b>ヘッダーテスト</b></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>こちらを参照</b></td>
    <td valign="top" align="left">
    <a href="../01_HTML_QuickForm/">HTML_QuickForm</a></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>パスワード</b></td>
    <td valign="top" align="left">
    <input size="20" maxlength="20" name="passwd" type="password" /></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b></b></td>
    <td valign="top" align="left">
    <input name="CLEAR" value="クリア" type="reset" /></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>好きな色:</b></td>
    <td valign="top" align="left"><select name="selColor">
    <option value="0">赤</option>
    <option value="1">オレンジ</option>
    <option value="2">黄</option>
    <option value="3">緑</option>
    <option value="4">青</option>
    <option value="5">紫</option>
    <option value="6">水色</option>
    <option value="7">白</option>
</select></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b></b></td>
    <td valign="top" align="left">
    <input name="SUBMIT" value="送信" type="submit" /></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>名前</b></td>
    <td valign="top" align="left">
    <input size="25" maxlength="25" name="name" type="text" /></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>感想:</b></td>
    <td valign="top" align="left"><textarea name="txaInp"></textarea></td>
  </tr>
</table>
</div>
</form>
■Mobileなら
こういう使い方もあります。
<?php
// Mobileでの使用例

class QF_Form {
    var $_form;
    var $mode_at;
    var $mode_va;

    function QF_Form($frmName, $argAction) {
        $this->_form = new HTML_QuickForm($frmName, 'post', $argAction);

        // DoCoMo
        if (ereg( "DoCoMo", $_SERVER["HTTP_USER_AGENT"] )) {
            $this->mode_at = "istyle";
            $this->mode_va = array("1", "2", "3", "4");

        //  ezweb
        } elseif (ereg( "UP\.Browser", $_SERVER["HTTP_USER_AGENT"] )) {
            if (ereg( "^UP\.Browser", $_SERVER["HTTP_USER_AGENT"] )) {
                header ("Location: http://www.okushin.co.jp/information/tanuki03/");
            } else {
                $this->mode_at = "format";
                $this->mode_va = array("*M", "*M", "*m", "*N");
            }

        //  vodephone
        } elseif (ereg( "J-PHONE", $_SERVER["HTTP_USER_AGENT"] )) {
            $this->mode_at = "mode";
            $this->mode_va = array("hiragana", "katakana", "alphabet", "numeric");

        //  L-mode
        } elseif (ereg( "L-mode", $_SERVER["HTTP_USER_AGENT"] )) {
            $this->mode_at = "istyle";
            $this->mode_va = array("1", "2", "3", "4");

        } else {
            $this->mode_at = "istyle";
            $this->mode_va = array("1", "2", "3", "4");
        }
    }

    // 「モード属性」
    function getMode_at() {
        return $this->mode_at;
    }

    // 「ひらがなモード」
    function getMode_va_hira() {
        return $this->mode_va[0];
    }
    // 「カタカナモード」
    function getMode_va_kata() {
        return $this->mode_va[1];
    }
    // 「アルファベット属性」
    function getMode_va_alph() {
        return $this->mode_va[2];
    }
    // 「数字記号属性」
    function getMode_va_num() {
        return $this->mode_va[3];
    }

}


class Test_Form extends QF_Form {

// フォームの項目を定義
    function setItems() {
        $this->_form->addElement('text', 'mail', '登録メールアドレス',
                array('size' => 15, 'maxlength' => 50,
                      $this->getMode_at() => $this->getMode_va_alph()));
        $this->_form->addElement('text', 'passwd', 'パスワード',
                array('size' => 15, 'maxlength' => 50,
                      $this->getMode_at() => $this->getMode_va_alph()));
        $this->_form->addElement('textarea', 'impressions', 'ご意見ご感想',
                array('cols' => 15, 'rows' => 5,
                      $this->getMode_at() => $this->getMode_va_hira()));
    }
?>



インフォメーションへ戻る 第1回 PHPのコードとHTMLタグ コラム
  第2回 HTML_QuickFormを使ってみよう
  第3回 HTML_QuickFormを使ってみよう(パート2) HTML_QuickForm Memo
  第4回 Smartyを使ってみよう HTML_QuickForm Menu
  第5回 HTML_QuickFormとSmartyを使ってみよう HTML_QuickForm Smarty Tips
  第6回 HTML_QuickFormとSmartyを使ってみよう(パート2) Mojavi Memo
  第7回 実践編 HTML_QuickFormとSmartyを使ってみてどうよ? Ajax Memo
  第8回 HTML_QuickForm_Controller大好き!
  第9回 Mojaviのまとめ
  第10回 Ajax使ってみました
  第11回 PHPEclipse開発環境設定
  第12回 Eclipse+PHPIDEインストール記
  第13回目 PHPIDEによるデバック
  第14回目 Selenium IDEを使ってみました
  第15回目 PDT(旧 PHPIDE)のインストール、デバック