質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Smarty

Smartyは、PHPアプリケーションで使用されるテンプレートエンジンです。

Q&A

0回答

4068閲覧

Smartyでお問い合わせフォームの作成方法

ttt777

総合スコア6

Smarty

Smartyは、PHPアプリケーションで使用されるテンプレートエンジンです。

0グッド

1クリップ

投稿2018/08/13 08:46

編集2022/01/12 10:55

Smartyでお問い合わせフォームを作っています。
入力後に入力内容に間違いがあれば、エラーメッセージを出し、
入力に間違いがなければ、確認画面を出し、
登録画面でデータベースに登録したいと思っております。

発生している問題

現在、入力内容のバリデーションがうまくかからず、苦戦しております。

index.php

<?php session_start(); // smartyを使うことを宣言 require( dirname( __FILE__ ).'/libs/Smarty.class.php' ); $smarty = new Smarty(); $smarty->template_dir = dirname( __FILE__ ).'/templates'; $smarty->compile_dir = dirname( __FILE__ ).'/templates_c'; // マスタDB(sele)に接続 try{ $dsn = 'mysql:host=localhost;dbname=sample'; $dbh = new PDO($dsn,'staff','password'); $dbh->query('SET NAMES UTF8'); $sql = $dbh->query('select * from sele'); //データを割り当てる $item = array(); while($data = $sql->fetch(PDO::FETCH_ASSOC)){ $item[] = $data; } // Smartyにitemsという名前で配列を渡す $smarty->assign('items',$item); }catch (PDOException $e){ echo 'Error:'.h($e->getMessage()); } //データベース接続終了 $dbh = null; //$_SESSION['postdata']にユーザが入力したデータがあるのでそれをアサイン if(isset($_SESSION['postdata'])){ $smarty->assign("q", $_SESSION['postdata'] ); }else{ $smarty->assign("q", array()); header("Location:confirm.php"); exit; } //エラー情報アサイン if( isset($_SESSION['err_info'])){ $smarty->assign("err", $_SESSION['err_info'] ); }else{ $smarty->assign("err", array()); } // テンプレート表示 $smarty->display('index.tpl'); // 1度セッションに入ったエラーメッセージを消去 unset($_SESSION['err_info']); if(isset($_POST['check']) ){ //echo 'a '; //unset($_SESSION['postdata']); InputCheck(); } function InputCheck(){ //セッション変数きれいに unset($_SESSION['postdata']); //入力チェック処理 if(isset($_POST['name']) || empty($_POST['name']) ){ $errInfo['name'] = 'お名前が入力されていません。'; } if(isset($_POST['tel']) || empty($_POST['tel']) ){ $errInfo['tel'] = '電話番号が入力されていません。'; } if(isset($_POST['email']) || empty($_POST['email']) ){ $errInfo['email'] = 'メールアドレスが入力されていません。'; } if(isset($_POST['post']) || empty($_POST['post']) ){ $errInfo['post'] = '郵便番号が入力されていません。'; } if(isset($_POST['address']) || empty($_POST['address']) ){ $errInfo['address'] = '住所が入力されていません。'; } if(isset($_POST['content']) || empty($_POST['content']) ){ $errInfo['content'] = 'お問い合わせの内容が指定されていません。'; } var_dump($errInfo); if( count($errInfo) > 0 ){ //POSTデータ,エラー情報をセッション変数に持たす $_SESSION['err_info'] = $errInfo; $_SESSION['postdata'] = $_POST; header("Location:index.php"); exit; } } ?>

index.tpl(出力部分)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>お問い合わせ</title> <link rel="stylesheet" href="comn/constyle.css"> </head> <body> <form action="index.php" method="post" id="inquiry"> <h1>お問い合わせ Smarty</h1>
<table summary="お問い合わせに関する入力項目名とその入力欄"> {foreach $items as $itemdata} <tr> <th><label for="genre">項目 {$itemdata.id}</label></th> <td><input type="checkbox" name="genre[]" value="{$itemdata.id}"> {$itemdata.chapter}</td> </tr> {/foreach} <tr> <th><label for="name">お名前 <span>必須</span> {if isset($err.name)}エラー:{$err.name}{/if}</label><br></th> <!--見出しのセル --> <td><input type="text" name="name" size="30" id="name" class="text1" value="{if isset($q.name)}{$q.name}{else}{/if}"></td><!-- 通常のセル --> </tr> <tr> <th><label for="tel">電話番号 <span>必須</span> {if isset($err.tel)}エラー:{$err.tel}{/if}</label></th> <td><input type="text" name="tel" size="30" id="tel" class="text2" value="{if isset($q.tel)}{$q.tel}{else}{/if}"></td> </tr> <tr> <th><label for="email">メールアドレス <span>必須</span> {if isset($err.email)}エラー:{$err.email}{/if}</label></th> <td><input type="text" name="email" size="30" id="email" class="text3" value="{if isset($q.email)}{$q.email}{else}{/if}"></td> </tr> <tr> <th><label for="post">郵便番号 <span>必須</span> {if isset($err.post)}エラー:{$err.post}{/if}</label></th> <td><input type="text" name="post" size="30" id="post" class="text4" value="{if isset($q.email)}{$q.email}{else}{/if}"></td> </tr> <tr> <th><label for="address">住所 <span>必須</span> {if isset($err.address)}エラー:{$err.address}{/if}</label></th> <td><input type="text" name="address" size="30" id="post" class="text5" value="{if isset($q.address)}{$q.address}{else}{/if}"></td> </tr> <tr> <th><label for="content">お問い合わせの内容 <span>必須</span> {if isset($err.content)}エラー:{$err.content}{/if}</label></th> <td><textarea name="content" cols="32" rows="5" id="content" class="text6">{if isset($q.content)}{$q.content}{else}{/if}</textarea></td> </tr> </table> <div class="submit"> <input type="submit" name="check" value="確認画面へ"> </div> </form> </body>
</html> ```

試したこと

https://jehupc.exblog.jp/15053257/
上記URLに載っている情報を参考にしました。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。
また、Smartyでの問い合わせフォームの作成の手順なども教えてもらうと助かります。
以上です。よろしくお願いします。

1 index.tplで入力フォーム作成
2 index.phpでマスタDBに接続し、入力画面に載せる選択項目作成
3 index.phpで入力項目のバリデーションを記す。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/08/13 08:52 編集

コードブロックはファイルごとに分けてください。 「Smartyでの問い合わせフォームの作成の手順なども教えてもらうと助かります。」質問範囲が広すぎるので、わからないことを具体的にしてください。
m.ts10806

2018/08/13 10:46

Smartyではなく純粋なPHPであれば可能なのか、実はそれも難しい のでは違います。SmartyはあくまでPHPのテンプレートエンジンなので、「PHP」のタグも追加した上で、何がどう分からないのか質問を具体的に絞ってください。あとできればインデントは調整してください。コードブロックが1つになっている上にインデントもない状態だと読む気が起きません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問