php で、入力画面とバリデーションエラー画面、確認画面、完了画面を同一ファイルで記述できないかと思い、下記のようなものを書いてみました。
php
1<?php 2if ( isset($_POST["submit"]) ) { 3 4 $errors = array(); 5 6 if ( $_POST["name"] == "" ) { $errors["name"] = "名前が未入力"; } 7 8 if ( !$errors ) { 9 $name = $_POST["name"]; 10 $flag_send = 1; //正常終了フラグ 11 } 12} 13 14print <<< EOM 15<!DOCTYPE html> 16<html lang="ja"> 17 <head> 18 <meta charset="utf-8"> 19 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 20 <meta name="viewport" content="width=device-width, initial-scale=1"> 21 <title>申込フォーム</title> 22 23 <!-- Bootstrap --> 24 <link href="css/bootstrap.min.css" rel="stylesheet"> 25 26 <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 27 <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 28 <!--[if lt IE 9]> 29 <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 30 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 31 <![endif]--> 32 </head> 33 <body> 34<div class="container"> 35 36<h1>申込フォーム</h1> 37EOM; 38 39if ( !isset($flag_send) ) { 40 print '<form method="post" action="" class="form-horizontal">'; 41 print '<div class="form-group">'; 42 print '<label class="col-sm-2 control-label" for="name">名前</label>'; 43 print '<div class="col-sm-3">'; 44 print '<input class="form-control" type="text" id="name" name="name" value="'.$_POST["name"].'" />'; 45 if ( isset($errors["name"]) ) { 46 print "<p class=\"text-danger\">".$errors["name"]."</p>"; 47 } 48 print '</div>'; 49 print '</div>'; 50 print '<div class="text-center"><input type="submit" name="submit" class="btn btn-default" value="確認"></div>'; 51 print '</form>'; 52} else { 53 print "処理完了"; 54} 55?>
下記2点、お伺いしたいです。
1)このような場合、確認画面、完了画面のURLをパラメータ表示などさせて判別できるようにするにはどのようにしたらよいのでしょうか。
2)print を頻繁に使用してHTMLを表示させていますが、あまりコードとして綺麗であるとは感じません。何か他にうまい記述方法などありますでしょうか。
以上、よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/02 00:59
2016/04/02 13:18