Title1

Title2

Title3

11-4 表單

一、分析所需表單欄位:

標題 title (text)網址 url (text)是否外連 target (radio)是否啟用 enable (radio)排序 sort(text) 
分類 kind="nav_home"

二、流程控制:

收到 op_form 進入表單(新增、編輯) => 送出一個 op_insert、op_update(寫入資料庫、更新資料庫)

三、函數:

op_form($sn)
 


########################################
# 表單
########################################
function op_form($sn="")
{
  global $mysqli,$smarty;

  #抓取預設值
  if($sn)
  {
    #編輯
    $DBV['op'] = "op_update";
    $DBV['form_title'] = "編輯選單";
  }else
  {
    #新增
    $DBV=array();

    $DBV['op'] = "op_insert";
    $DBV['form_title'] = "新增選單";
  }

  //預設值設定
  //標題 title (text)、網址 url (text)、是否外連 target (radio)、是否啟用 enable (radio)、排序 sort(text)

  $DBV['sn']     = (isset($DBV['sn']))     ? $DBV['sn']     : "";
  $DBV['title']  = (isset($DBV['title']))  ? $DBV['title']  : "";
  $DBV['enable'] = (isset($DBV['enable'])) ? $DBV['enable'] : "1";
  $DBV['target'] = (isset($DBV['target'])) ? $DBV['target'] : "0";
  $DBV['url']    = (isset($DBV['url']))    ? $DBV['url']    : "";
  $DBV['sort']   = (isset($DBV['sort']))   ? $DBV['sort']   : "";


  $smarty->assign("DBV", $DBV);
  return;
}

四、在樣板製作表單


    {* nav_m.php and op_form *}
    {if $WEB.file_name == "nav_m.php" and $op == "op_form"}
      <div class="container" style="margin-top:20px;">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">{$DBV.form_title}</h3>
          </div>
          <div class="panel-body">

            <form action="nav_m.php" method="post">

              <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                    <label>標題</label>
                    <input type="text" class="form-control" id="title " name="title" value="{$DBV.title}" placeholder="標題">
                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label style="display:block;">外連狀態</label>

                    <input type='radio' name='target' id='target_1' value='1' {if $DBV.target==1}checked{/if} >
                    <label for='target_1'>是</label>&nbsp;&nbsp;

                    <input type='radio' name='target' id='target_0' value='0' {if $DBV.target==0}checked{/if}>
                    <label for='target_0'>否</label>

                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label style="display:block;">啟用狀態</label>

                    <input type='radio' name='enable' id='enable_1' value='1' {if $DBV.enable==1}checked{/if} >
                    <label for='enable_1'>是</label>&nbsp;&nbsp;

                    <input type='radio' name='enable' id='enable_0' value='0' {if $DBV.enable==0}checked{/if}>
                    <label for='enable_0'>否</label>

                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label>排序</label>
                    <input type="text" class="form-control" id="sort"  name="sort"  value="{$DBV.sort}" placeholder="排序">
                  </div>
                </div>
              </div>


              <div class="form-group">
                <label>網址</label>
                <input type="text" class="form-control" id="url"  name="url"  value="{$DBV.url}" placeholder="網址">
              </div>

              <!--用來控制程式流程-->
              <input type="hidden" name="op" value="{$DBV.op}">
              <input type="hidden" name="sn" value="{$DBV.sn}">
              <button type="submit" class="btn btn-default">送出</button>
            </form>

          </div>
        </div>
      </div>

    {/if}