前回までのソースコードを元に、新規登録画面の一連の動きを書きます。
/**
* 登録
*/
function regist($error_message_flg = null)
{
if ($error_message_flg == false) {
$sample_name = esc_attr($_REQUEST["sample_name"]);
}
if (isset($_REQUEST["form_id"])) {
//確認画面の「戻る」ボタンから遷移してきた場合
$form_id = esc_attr($_REQUEST["form_id"]);
$sample_name = esc_attr($_REQUEST["sample_name"]);
$sample_text = esc_attr($_REQUEST["sample_text"]);
$sample_check = $_REQUEST["sample_check"];
$sample_radio = esc_attr($_REQUEST["sample_radio"]);
$sample_select = esc_attr($_REQUEST["sample_select"]);
$sample_textarea = esc_attr($_REQUEST["sample_textarea"]);
//チェックボックスの判定
foreach ($sample_check as $key => $value) {
$sample_check_checked[$value] = "checked";
}
//ラジオボタンの判定
for ($i=0; $i<3; $i++) {
if ($sample_radio == $i) {
$sample_radio_checked[$i] = "checked";
}
}
//セレクトボックスの判定
$sample_select_array = array("a", "b", "c", "d", "e");
foreach ($sample_select_array as $key => $value) {
if ($sample_select == $value) {
$sample_select_selected[$key] = "selected";
}
}
$create_date = esc_attr($_REQUEST["create_date"]);
} else {
//一覧画面から「編集」ボタンを押下した場合
//押されたボタンのIDを取得する
if (array_search("編集", $_REQUEST["submit"]["edit"])) {
$form_id = array_search("編集", $_REQUEST["submit"]["edit"]);
}
global $wpdb;
$tbl_name = $wpdb->prefix . 'sample_mst';
$sql = "SELECT * FROM {$tbl_name} WHERE id = %d;";
$prepared = $wpdb->prepare($sql, $form_id);
$rows = $wpdb->get_results($prepared, ARRAY_A);
$sample_name = $rows[0]["sample_name"];
$sample_text = $rows[0]["sample_text"];
$sample_check = $rows[0]["sample_check"];
$sample_radio = $rows[0]["sample_radio"];
$sample_select = $rows[0]["sample_select"];
$sample_textarea = $rows[0]["sample_textarea"];
$sample_check = explode(",", $sample_check);
//チェックボックスの判定
foreach ($sample_check as $key => $value) {
$sample_check_checked[$value] = "checked";
}
//ラジオボタンの判定
for ($i=0; $i<3; $i++) {
if ($sample_radio == $i) {
$sample_radio_checked[$i] = "checked";
}
}
//セレクトボックスの判定
$sample_select_array = array("a", "b", "c", "d", "e");
foreach ($sample_select_array as $key => $value) {
if ($sample_select == $value) {
$sample_select_selected[$key] = "selected";
}
}
$create_date = $rows[0]["create_date"];
}
echo <<< EOL
<form action="" method="post">
<h2>データ登録</h2>
EOL;
if (strlen($error_message_flg)) {
echo "<div class='updated fade'><p><strong>";
echo _e('NAMEを入力してください');
echo "</strong></p></div>";
}
echo <<< EOL
<div class="wrap">
<form action="" method="post">
<h2>データ編集</h2>
<div class="wrap">
<table class="wp-list-table widefat striped posts">
<tr>
<td>ID</td>
<td>{$form_id}</td>
</tr>
<tr>
<td>NAME</td>
<td>
<input type="text" name="sample_name" value="{$sample_name}">
</td>
</tr>
<tr>
<td>テキスト</td>
<td>
<input type="text" name="sample_text" value="{$sample_text}">
</td>
</tr>
<tr>
<td>チェックボックス</td>
<td>
チェック値1<input type="checkbox" name="sample_check[]" value="0" {$sample_check_checked[0]}>
チェック値2<input type="checkbox" name="sample_check[]" value="1" {$sample_check_checked[1]}>
チェック値3<input type="checkbox" name="sample_check[]" value="2" {$sample_check_checked[2]}>
</td>
</tr>
<tr>
<td>ラジオボタン</td>
<td>
ラジオ値1<input type="radio" name="sample_radio" value="0" {$sample_radio_checked[0]}>
ラジオ値2<input type="radio" name="sample_radio" value="1" {$sample_radio_checked[1]}>
ラジオ値3<input type="radio" name="sample_radio" value="2" {$sample_radio_checked[2]}>
</td>
</tr>
<tr>
<td>セレクトボックス</td>
<td>
<select name="sample_select">
<option value="a" {$sample_select_selected[0]}>選択a</option>
<option value="b" {$sample_select_selected[1]}>選択b</option>
<option value="c" {$sample_select_selected[2]}>選択c</option>
<option value="d" {$sample_select_selected[3]}>選択d</option>
<option value="e" {$sample_select_selected[4]}>選択e</option>
</select>
</td>
</tr>
<tr>
<td>テキストエリア</td>
<td>
<textarea name="sample_textarea" cols="30" rows="5">{$sample_textarea}</textarea>
</td>
</tr>
<tr>
<td>登録日時</td>
<td>{$create_date}</td>
</tr>
</table>
<input type='submit' name='submit[regist_check]' class='button-primary' value='登録内容を確認する' />
<input type='submit' name='submit[]' class='button-primary' value='戻る' />
<input type="hidden" name="form_id" value="{$form_id}">
<input type="hidden" name="create_date" value="{$create_date}">
</div>
</form>
EOL;
}
/**
* 登録確認
*/
function regist_check()
{
if (!strlen($_REQUEST["sample_name"])) {
self::regist(false);
return;
}
$sample_name = esc_attr($_REQUEST["sample_name"]);
$sample_name = esc_attr($_REQUEST["sample_name"]);
$sample_text = esc_attr($_REQUEST["sample_text"]);
$sample_check = $_REQUEST["sample_check"];
$sample_radio = esc_attr($_REQUEST["sample_radio"]);
$sample_select = esc_attr($_REQUEST["sample_select"]);
$sample_textarea = esc_attr($_REQUEST["sample_textarea"]);
//チェックボックスの値を処理
if (is_array($_REQUEST["sample_check"])) {
foreach ($_REQUEST["sample_check"] as $key => $value) {
$disp_checkbox .= "チェック値" . $value . "<br />";
$hidden_checkbox .= '<input type="hidden" name="sample_check[]" value="' . $value . '">';
}
}
//テキストエリア整形(表示用)
$sample_textarea_disp = nl2br($sample_textarea);
//データ一覧
echo <<< EOL
<form action="" method="post">
<h2>データ編集確認</h2>
<div class="wrap">
<table class="wp-list-table widefat striped posts">
<tr>
<td>ID</td>
<td>{$form_id}</td>
</tr>
<tr>
<td>NAME</td>
<td>{$sample_name}</td>
</tr>
<tr>
<td>テキスト</td>
<td>{$sample_text}</td>
</tr>
<tr>
<td>チェックボックス</td>
<td>
{$disp_checkbox}
</td>
</tr>
<tr>
<td>ラジオボタン</td>
<td>{$sample_radio}</td>
</tr>
<tr>
<td>セレクトボックス</td>
<td>{$sample_select}</td>
</tr>
<tr>
<td>テキストエリア</td>
<td>{$sample_textarea_disp}</td>
</tr>
</table>
<input type="hidden" name="form_id" value="{$form_id}">
<input type="hidden" name="sample_name" value="{$sample_name}">
<input type="hidden" name="sample_text" value="{$sample_text}">
{$hidden_checkbox}
<input type="hidden" name="sample_radio" value="{$sample_radio}">
<input type="hidden" name="sample_select" value="{$sample_select}">
<input type="hidden" name="sample_textarea" value="{$sample_textarea}">
<input type="hidden" name="create_date" value="{$create_date}">
</div>
<input type='submit' name='submit[regist_exec]' class='button-primary' value='登録する' />
<input type='submit' name='submit[regist]' class='button-primary' value='戻る' />
</form>
EOL;
}
/**
* 登録実行
*/
function regist_exec()
{
global $wpdb;
$sample_name = esc_attr($_REQUEST["sample_name"]);
$sample_text = esc_attr($_REQUEST["sample_text"]);
$sample_radio = esc_attr($_REQUEST["sample_radio"]);
$sample_select = esc_attr($_REQUEST["sample_select"]);
$sample_textarea = esc_attr($_REQUEST["sample_textarea"]);
//チェックボックスの値を処理
if (is_array($_REQUEST["sample_check"])) {
foreach ($_REQUEST["sample_check"] as $key => $value) {
$sample_check .= $value . ",";
}
}
//投稿を登録
$table_name = $wpdb->prefix . 'sample_mst';
$result = $wpdb->insert(
$table_name,
array(
'sample_name' => $sample_name,
'sample_text' => $sample_text,
'sample_check' => $sample_check,
'sample_radio' => $sample_radio,
'sample_select' => $sample_select,
'sample_textarea' => $sample_textarea,
'create_date' => current_time('mysql')
)
);
//データ一覧
echo <<< EOL
<form action="" method="post">
<h2>データ登録</h2>
<div class='updated fade'><p><strong>
EOL;
echo _e('登録が完了しました');
echo <<<EOL
</strong></p></div>
<input type='submit' name='submit[]' class='button-primary' value='戻る' />
EOL;
echo "</form>";
}
更新系ですので、修正の処理を同じような流れになります。
登録フォームがあり、確認画面があり、完了メッセージ画面があります。